-
Notifications
You must be signed in to change notification settings - Fork 374
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
Define bli_pthread_switch_t
+ matching API
#634
Conversation
Details: - Defined and implemented a new pthread-like abstract datatype and API in bli_pthread.c/.h. The new type, bli_pthread_switch_t, is similar to bli_pthread_once_t in some respects, except that it may be moved back and forth between two states via two user-supplied functions. The idea is that like a switch in your home that controls a light or ceiling fan, it can either be on or off. The switch starts in the off state. Moving from one state to the other (on to off; off to on) causes some action (i.e., a startup or shutdown function) to be executed. Trying to move from one state to the same state (on to on; off to off) is safe in that it results in no action. Attempted state changes are thread-safe, as the state is protected via a mutex internal to each switch instance. Naturally, unlike bli_pthread_once(), the API for bli_pthread_switch_t contains both _on() and _off() interfaces. Also, unlike bli_pthread_once(), the _on() and _off() functions return error codes so that the 'int' error code returned from the user- supplied functions may be passed back to the caller. Thanks to Devin Matthews for his input and feedback on this feature.
Honestly, this would seem so useful for things like library initialization that I'm surprised it's not part of POSIX threads already. |
👍 |
Whoops. Looks like I reversed the conditional for changing the switch: // If the init() function succeeded, turn the switch on;
// otherwise, leave the switch off.
if ( r_val != 0 )
sw->status = 1; Should be |
Just out of curiosity, can this help removing the need of (I would squash the two commits together) |
That is where we are headed, yes. 🙂 |
👍 |
Details: - Replaced the previous implementations of bli_init_once() and bli_finalize_once() -- both of which used bli_pthread_once() -- with ones that rely upon bli_pthread_switch_on() and _switch_off(), respectively. - Updated the return types of _init_apis() and _finalize_apis() to match the function pointer type required by bli_pthread_switch_on() and _switch_off(). - Comment/whitespace updates.
Details: - Defined and implemented a new pthread-like abstract datatype and API in bli_pthread.c. The new type, bli_pthread_switch_t, is similar to bli_pthread_once_t in some respects. The idea is that like a switch in your home that controls a light or ceiling fan, it can either be on or off. The switch starts in the off state. Moving from one state to the other (on to off; off to on) causes some action (i.e., a startup or shutdown function) to be executed. Trying to move from one state to the same state (on to on; off to off) is safe in that it results in no action. Unlike bli_pthread_once(), the API for bli_pthread_switch_t contains both _on() and _off() interfaces. Also, unlike the _once() function, the _on() and _off() functions return error codes so that the 'int' error code returned from the startup or shutdown functions may be passed back to the caller. Thanks to Devin Matthews for his input and feedback on this feature. - Replaced the previous implementation of bli_init_once() and bli_finalize_once() -- both of which used bli_pthread_once() -- with ones that rely upon bli_pthread_switch_on() and _switch_off(), respectively. This also required updating the return types of _init_apis() and _finalize_apis() to match the function pointer type required by bli_pthread_switch_on()/_switch_off(). - Comment updates. - (cherry picked from commit 4603324)
This PR implements a pthread-like switch type that can be used to safely move between two states. Similar to
pthread_once()
, the user-supplied init function will never be invoked twice in immediate succession, even if the caller attempts to turn the switch "on" repeatedly. However, unlikepthread_once()
, a switch may be re-initialized (turned on) as long as it is first de-initialized (turned off).@devinamatthews Let me know if you see anything problematic.