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

Define bli_pthread_switch_t + matching API #634

Merged
merged 3 commits into from
May 19, 2022
Merged

Define bli_pthread_switch_t + matching API #634

merged 3 commits into from
May 19, 2022

Conversation

fgvanzee
Copy link
Member

@fgvanzee fgvanzee commented May 18, 2022

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, unlike pthread_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.

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.
@fgvanzee
Copy link
Member Author

Honestly, this would seem so useful for things like library initialization that I'm surprised it's not part of POSIX threads already.

@devinamatthews
Copy link
Member

👍

@fgvanzee
Copy link
Member Author

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 r_val == 0, since 0 here means success.

@hominhquan
Copy link
Contributor

Just out of curiosity, can this help removing the need of pthread_once() ?

(I would squash the two commits together)

@fgvanzee
Copy link
Member Author

Just out of curiosity, can this help removing the need of pthread_once() ?

(I would squash the two commits together)

That is where we are headed, yes. 🙂

@hominhquan
Copy link
Contributor

👍

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.
@fgvanzee fgvanzee marked this pull request as ready for review May 19, 2022 14:33
@fgvanzee fgvanzee merged commit 4603324 into master May 19, 2022
@fgvanzee fgvanzee deleted the pthread_switch branch July 13, 2022 23:32
fgvanzee added a commit that referenced this pull request Oct 26, 2023
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants