Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux shared library to support multi threaded application callbacks

I need to create a shared library which exposes a set of APIs which will be used by multiple processes which might have more than one thread from where the APIs get called.

This shared library in turn uses another 3rd party shared library to which I need to register a callback. The 3rd party library calls the registered callback from a different thread.

I want to know how to block a thread on a call of API defined in my library and release it when I get the callback from the 3rd party library. This locking should not block other thread from calling the same API...!

I'm using pthread library to create my library.

Psudo Code:

My Library:

int lib_init()
{
    register_callback(callback);
}

int lib_deinit()
{
    unregister_callback();
}

int callback(void *)
{
    <unblock the functions>
}

int function1(int, int)
{
    perform_action();
    <should block till I get a callback from 3rd party library>
}

int function2(char, char)
{
    perform_action();
    <should block till I get a callback from 3rd party library>
}

3rd Party Library:

int register_callback(callback)
{
    ....
    launch_thread();
    ....
}

int unregister_callback()
{
    ....
    terminate_thread();
    ....
}

int perform_action()
{
    /* queue action */
}

void* thread(void*arg)
{
    /* perform the action & invoke callback */
    invoke_callback();
}

Application:

main()
{
    init_lib();
    ....
    create_new_thread();
    ....
    function1(10, 20);
    ....
    function2('c', 'd');
}

another_thread()
{
    function2('a', 'b');
    ....
}

The exact problem I'm not able to solve is what(how) locking mechanism I need to put in place to block the calls to functions defined in my library & wait for callback from 3rd party library provided my library shall be used in a multi-process & multi-threaded environment.

like image 975
Kishore Kundanagurthy Avatar asked Jul 07 '26 13:07

Kishore Kundanagurthy


1 Answers

Using the plain pthreads interface, you would use condition variables:

int lib_init()
{
    pthread_cond_init(&cond, NULL);
    pthread_mutex_init(&mutex, NULL);
    register_callback(callback);
}

int lib_deinit()
{
    unregister_callback();
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
}

int callback(void *p)
{
    pthread_mutex_lock(&mutex);
    result[??] = p;
    pthread_cond_broadcast(&cond);
    pthread_mutex_unlock(&mutex);
}

int function1(int, int)
{
    result[??] = NULL;
    perform_action();

    pthread_mutex_lock(&mutex);
    while (result[??] == NULL)
        pthread_cond_wait(&cond, &mutex);
    pthread_mutex_unlock(&mutex);
}

int function2(char, char)
{
    result[??] = NULL;
    perform_action();


    pthread_mutex_lock(&mutex);
    while (result[??] == NULL)
        pthread_cond_wait(&cond, &mutex);
    pthread_mutex_unlock(&mutex);
}

pthread_cond_wait() unlocks the mutex while it waits.

The result[??] is a stand-in for some method, which you'll have to come up with, to link a particular callback invocation to the specific function1() or function2() call(s) it is relevant to.

like image 180
caf Avatar answered Jul 10 '26 01:07

caf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!