Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.5KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008-2020 Jonathan Moore Liles (as "Non-Session-Manager") */
  3. /* Copyright (C) 2020- Nils Hilbricht */
  4. /* */
  5. /* This file is part of New-Session-Manager */
  6. /* */
  7. /* New-Session-Manager is free software: you can redistribute it and/or modify */
  8. /* it under the terms of the GNU General Public License as published by */
  9. /* the Free Software Foundation, either version 3 of the License, or */
  10. /* (at your option) any later version. */
  11. /* */
  12. /* New-Session-Manager is distributed in the hope that it will be useful, */
  13. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  14. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  15. /* GNU General Public License for more details. */
  16. /* */
  17. /* You should have received a copy of the GNU General Public License */
  18. /* along with New-Session-Manager. If not, see <https://www.gnu.org/licenses/>.*/
  19. /*******************************************************************************/
  20. #include "Thread.hpp"
  21. #include <assert.h>
  22. #include <string.h>
  23. pthread_key_t Thread::_current = 0;
  24. Thread::Thread ( )
  25. {
  26. _thread = 0;
  27. _running = false;
  28. _name = 0;
  29. }
  30. Thread::Thread ( const char *name )
  31. {
  32. _thread = 0;
  33. _running = false;
  34. _name = name;
  35. }
  36. void
  37. Thread::init ( void )
  38. {
  39. pthread_key_create( &_current, NULL );
  40. }
  41. bool
  42. Thread::is ( const char *name )
  43. {
  44. return ! strcmp( Thread::current()->name(), name );
  45. }
  46. /** to be used by existing threads (that won't call clone()) */
  47. void
  48. Thread::set ( const char *name )
  49. {
  50. _thread = pthread_self();
  51. _name = name;
  52. _running = true;
  53. pthread_setspecific( _current, (void*)this );
  54. }
  55. Thread *
  56. Thread::current ( void )
  57. {
  58. return (Thread*)pthread_getspecific( _current );
  59. }
  60. struct thread_data
  61. {
  62. void *(*entry_point)(void *);
  63. void *arg;
  64. void *t;
  65. };
  66. void *
  67. Thread::run_thread ( void *arg )
  68. {
  69. thread_data td = *(thread_data *)arg;
  70. delete (thread_data*)arg;
  71. pthread_setspecific( _current, td.t );
  72. ((Thread*)td.t)->_running = true;
  73. void * r = td.entry_point( td.arg );
  74. ((Thread*)td.t)->_running = false;
  75. return r;
  76. }
  77. bool
  78. Thread::clone ( void *(*entry_point)(void *), void *arg )
  79. {
  80. assert( ! _thread );
  81. thread_data *td = new thread_data;
  82. td->entry_point = entry_point;
  83. td->arg = arg;
  84. td->t = this;
  85. if ( pthread_create( &_thread, NULL, run_thread, td ) != 0 )
  86. return false;
  87. return true;
  88. }
  89. void
  90. Thread::detach ( void )
  91. {
  92. pthread_detach( _thread );
  93. _thread = 0;
  94. }
  95. void
  96. Thread::cancel ( void )
  97. {
  98. pthread_cancel( _thread );
  99. _thread = 0;
  100. }
  101. void
  102. Thread::join ( void )
  103. {
  104. if ( _thread != 0 )
  105. {
  106. /* not joined yet, go ahead. */
  107. pthread_join( _thread, NULL );
  108. }
  109. _thread = 0;
  110. }
  111. /* never call this unless some other thread will be calling 'join' on
  112. * this one, otherwise, running() will return true even though the
  113. * thread is dead */
  114. void
  115. Thread::exit ( void *retval )
  116. {
  117. _running = false;
  118. pthread_exit( retval );
  119. }