/[svn]/linuxsampler/trunk/src/common/Thread.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/common/Thread.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3763 by schoenebeck, Fri Jun 23 12:24:58 2017 UTC revision 3764 by schoenebeck, Sun Apr 5 21:41:27 2020 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6   *   Copyright (C) 2005 - 2017 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2020 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 26  Line 26 
26  #if HAVE_CONFIG_H  #if HAVE_CONFIG_H
27  # include <config.h>  # include <config.h>
28  #endif  #endif
29    #include "global_private.h"
30    
31    #include <list>
32  #if DEBUG  #if DEBUG
33  # include <assert.h>  # include <assert.h>
34  #endif  #endif
35    
36    #if !CONFIG_PTHREAD_TESTCANCEL
37    # warning No pthread_testcancel() available: this may lead to mutex dead locks when threads are stopped!
38    #endif
39    
40  // this is the minimum stack size a thread will be spawned with  // this is the minimum stack size a thread will be spawned with
41  // if this value is too small, the OS will allocate memory on demand and  // if this value is too small, the OS will allocate memory on demand and
42  // thus might lead to dropouts in realtime threads  // thus might lead to dropouts in realtime threads
43  // TODO: should be up for testing to get a reasonable good value  // TODO: should be up for testing to get a reasonable good value
44  #define MIN_STACK_SIZE          524288  #define MIN_STACK_SIZE          524288
45    
46    #if !defined(WIN32)
47    static thread_local std::list<int> cancelStates;
48    #endif
49    
50  namespace LinuxSampler {  namespace LinuxSampler {
51    
52  Thread::Thread(bool LockMemory, bool RealTime, int PriorityMax, int PriorityDelta) {  Thread::Thread(bool LockMemory, bool RealTime, int PriorityMax, int PriorityDelta) {
# Line 489  void Thread::pthreadDestructor(void* thr Line 499  void Thread::pthreadDestructor(void* thr
499  }  }
500  #endif  #endif
501    
502    /**
503     * Allow or prohibit whether the calling thread may be cancelled, remember its
504     * previous setting on a stack.
505     *
506     * @discussion The POSIX standard defines certaing system calls as implied
507     * thread cancellation points. For instance when a thread calls @c usleep(), the
508     * thread would immediately terminate if the thread was requested to be stopped.
509     * The problem with this is that a thread typically has critical sections where
510     * it may not simply terminate unexpectedly, e.g. if a thread is currently
511     * holding a mutex lock and then would call @c usleep() this may end up in a
512     * dead lock, since the lock would then never be unlocked again. For that reason
513     * a thread should first disable itself being cancelable before entering a
514     * critical section by calling @c pushCancelable(false) and it should
515     * re-enable thread cancelation by calling @c popCancelable() after having left
516     * the critical section(s). Refer to the following link for a list of functions
517     * being defined as implied cancelation points:
518     * http://man7.org/linux/man-pages/man7/pthreads.7.html
519     *
520     * @b NOTE: This method is currently not implemented for Windows yet!
521     *
522     * @param cancel - @c true: thread may be cancelled, @c false: thread may not
523     *                 be cancelled until re-enabled with either pushCancelable() or
524     *                 popCancelable()
525     * @see popCancelable() as counter part
526     */
527    void Thread::pushCancelable(bool cancel) {
528        #if defined(WIN32)
529        //TODO: implementation for Windows
530        #else
531        int old;
532        pthread_setcancelstate(cancel ? PTHREAD_CANCEL_ENABLE : PTHREAD_CANCEL_DISABLE, &old);
533        cancelStates.push_back(old);
534        #endif
535    }
536    
537    /**
538     * Restore previous cancellation setting of calling thread by restoring it from
539     * stack.
540     *
541     * @b NOTE: This method is currently not implemented for Windows yet!
542     *
543     * @see pushCancelable() for details
544     */
545    void Thread::popCancelable() {
546        #if defined(WIN32)
547        //TODO: implementation for Windows
548        #else
549        int cancel = cancelStates.back();
550        cancelStates.pop_back();
551        pthread_setcancelstate(cancel ? PTHREAD_CANCEL_ENABLE : PTHREAD_CANCEL_DISABLE, NULL);
552        #endif
553    }
554    
555    /**
556     * Return this thread's name (intended just for debugging purposes).
557     *
558     * @b NOTE: This method is currently not implemented for Windows yet!
559     */
560    std::string Thread::name() {
561        #if defined(WIN32)
562        //TODO: implementation for Windows
563        return "not implemented";
564        #else
565        char buf[16] = {};
566        pthread_getname_np(__thread_id, buf, 16);
567        std::string s = buf;
568        if (s.empty())
569            s = "tid=" + ToString(__thread_id);
570        return s;
571        #endif
572    }
573    
574    /**
575     * Return calling thread's name (intended just for debugging purposes).
576     *
577     * @b NOTE: This method is currently not implemented for Windows yet!
578     */
579    std::string Thread::nameOfCaller() {
580        #if defined(WIN32)
581        //TODO: implementation for Windows
582        return "not implemented";
583        #else
584        char buf[16] = {};
585        pthread_getname_np(pthread_self(), buf, 16);
586        std::string s = buf;
587        if (s.empty())
588            s = "tid=" + ToString(pthread_self());
589        return s;
590        #endif
591    }
592    
593    /**
594     * Give calling thread a name (intended just for debugging purposes).
595     *
596     * @b NOTE: POSIX defines a limit of max. 16 characters for @a name.
597     *
598     * @b NOTE: This method is currently not implemented for Windows yet!
599     *
600     * @param name - arbitrary, i.e. human readable name for calling thread
601     */
602    void Thread::setNameOfCaller(std::string name) {
603        #if defined(WIN32)
604        //TODO: implementation for Windows
605        #elif __APPLE__
606        pthread_setname_np(name.c_str());
607        #else // Linux, NetBSD, FreeBSD, ...
608        pthread_setname_np(pthread_self(), name.c_str());
609        #endif
610    }
611    
612  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.3763  
changed lines
  Added in v.3764

  ViewVC Help
Powered by ViewVC