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.

141 lines
3.2KB

  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. return td.entry_point( td.arg );
  72. }
  73. bool
  74. Thread::clone ( void *(*entry_point)(void *), void *arg )
  75. {
  76. assert( ! _thread );
  77. thread_data *td = new thread_data;
  78. td->entry_point = entry_point;
  79. td->arg = arg;
  80. td->t = this;
  81. if ( pthread_create( &_thread, NULL, run_thread, td ) != 0 )
  82. return false;
  83. return true;
  84. }
  85. void
  86. Thread::detach ( void )
  87. {
  88. pthread_detach( _thread );
  89. _thread = 0;
  90. }
  91. void
  92. Thread::cancel ( void )
  93. {
  94. pthread_cancel( _thread );
  95. _thread = 0;
  96. }
  97. void
  98. Thread::join ( void )
  99. {
  100. pthread_join( _thread, NULL );
  101. _thread = 0;
  102. }
  103. /* never call this unless some other thread will be calling 'join' on
  104. * this one, otherwise, running() will return true even though the
  105. * thread is dead */
  106. void
  107. Thread::exit ( void *retval )
  108. {
  109. _running = false;
  110. pthread_exit( retval );
  111. }