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.

149 lines
3.4KB

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