Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate re_main in to custom main loop #117

Open
ajunca opened this issue May 31, 2018 · 7 comments
Open

Integrate re_main in to custom main loop #117

ajunca opened this issue May 31, 2018 · 7 comments
Labels
question This is a question

Comments

@ajunca
Copy link

ajunca commented May 31, 2018

Hi,

For several days I tried to run rawrtc on top of my game loop without luck. I'm not able to find any resource, documentation or example on how to proceed with this (all examples are using re_main in the main thread). My aproches has been so far trying to run re_main in another thread, and it's "kind" of working but I think I'm not doing it right.

Someone know how to accomplish this? Some example or direction will be really welcome because I'm kind of stuck.

Thanks.

@lgrahl lgrahl added the question This is a question label May 31, 2018
@lgrahl
Copy link
Member

lgrahl commented May 31, 2018

Yup, docs are an issue.

Here is an example how re can be used in a thread. You are probably aware that you will have to acquire some lock when communicating with that thread from another one. You can do so with re_thread_enter and unlock via re_thread_leave but be aware those are not reentrant by default. AFAIK you can set your own (reentrant) mutex by using re_set_mutex.

As you can see in the examples, I have not used RAWRTC with threads so far, so I would be interested to see if it works out for you.

If you are using a different event loop, an alternative would be to make it compatible to the re event loop or the other way around. @ruengeler has applied some patches to re to make it run in a libuv event loop but this hasn't been merged so far. Still, if you're interested, ping me.

@ajunca
Copy link
Author

ajunca commented Jun 1, 2018

Thank you for the response.

I've been trying with your example but I'm getting problems locking re. I'm not sure witch functions i have to re_thread_enter/re_thread_leave. Rawrtc does this internally in some places? I'm also getting this output error "main: re_lock: Resource deadlock avoided", probably for what you say that is not reentrant right? Any examples about re_set_mutex?

If you have some more information/examples about this it will be awesome.

About what you said on using a different event loop, I was looking how re_main was working internally and it seems a bit of heavy lifting to implement it in my event loop. Also I'm not really sure about performance on running in the same main thread, I'm a bit scared of some unwanted waitings inside rawrtc/re.

@lgrahl
Copy link
Member

lgrahl commented Jun 1, 2018

The master branch of RAWRTC does some locking internally (at the moment at least, I'm planning to remove this). Since it also uses re_thread_enter and re_thread_leave, this is probably why you're seeing the warning. If you set a reentrant mutex, this should (hopefully) resolve the problem.

Check out re's docs or the code. The mutex is of type pthread_mutex_t.

Example (untested):

    int err;
    pthread_mutex_t mutex;
    pthread_mutexattr_t mutex_attribute;

    // Initialise and set mutex attribute
    err = pthread_mutexattr_init(&mutex_attribute);
    if (err) {
        DEBUG_WARNING("Failed to initialise mutex attribute, reason: %m\n", err);
        return rawrtc_error_to_code(err);
    }
    err = pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE);
    if (err) {
        DEBUG_WARNING("Failed to set mutex attribute, reason: %m\n", err);
        return rawrtc_error_to_code(err);
    }

    // Initialise mutex
    err = pthread_mutex_init(&mutex, &mutex_attribute);
    if (err) {
        DEBUG_WARNING("Failed to initialise mutex, reason: %m\n", err);
        return rawrtc_error_to_code(err);
    }

    // Apply the mutex
    re_set_mutex(&mutex);

    // Store that mutex somewhere
    // ...

@ajunca
Copy link
Author

ajunca commented Jun 6, 2018

I could make a little test to run this way and for now it seems to work. I will have to develop further to keep testing.

By the way, at some point I will have compile this on windows (now I'm on arch linux), and going throw the code it seems that it is only implemented for pthreads right? I have to supply a custom pthread library for windows to work?

Thanks a lot for such help.

@lgrahl
Copy link
Member

lgrahl commented Jun 6, 2018

To be honest, I don't know since I never tried to compile it on Windows. But the code of re is limited to pthread, so I assume you will have to find a pthread-compatible library for Windows.

@lgrahl lgrahl mentioned this issue Aug 31, 2018
@Globik
Copy link

Globik commented Aug 31, 2018

Libuv has interface for epoll / poll fd communications to other events outside of libuv ecosystem.

@Globik
Copy link

Globik commented Feb 2, 2019

@junky89 or could be used the mqueue from libre lib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question This is a question
Projects
None yet
Development

No branches or pull requests

3 participants