-
Notifications
You must be signed in to change notification settings - Fork 256
Pthread
libonion has full support for pthreading.
This support has two versions, basic fork model, where each new thread is made per each new connection (so efficient throught keep-alive, but no more), and a pool mode.
The mode can be selected passing the proper flag to the onion object creation, O_THREADED or O_POOL. Actually O_POOL is implemented using epoll, with each thread waiting a thread to be ready, so it is the bit OR mask of O_POLL|O_THREADED.
Internally all data is decoupled on the request object and there is no global state, so its inherently thread safe. There anyway one point where libonion can not ensure this thread safety and thats in user code.
If it is needed thread safety on program code normal pthread elements can be used: semaphores, mutex... Thread safely via decoupling of data should be looked after.
There is anyway one onion construct that is thread aware, and that's the dictionaries. They are widely used thought all the code, and it requires the thread safeness to allow to use them in the sessions.
Threads can ask the request to get the session. Sessions are not thread safe, but give the means to be. Dictionaries can be used as locks, as they have onion_dict_lock_read, onin_dict_lock_write and onion_dict_unlock functions.
So to use safely a session user must read lock it when reading, and write lock when writing. Finally they should release the lock.
onion_dict *session=onion_request_get_session_dict(request);
onion_dict_lock_read(session);
...
onion_dict_unlock(session);