1 /*
2  *  $Id$
3  *
4  * SocketAPI implementation for the sctplib.
5  * Copyright (C) 1999-2020 by Thomas Dreibholz
6  *
7  * Realized in co-operation between
8  * - Siemens AG
9  * - University of Essen, Institute of Computer Networking Technology
10  * - University of Applied Sciences, Muenster
11  *
12  * Acknowledgement
13  * This work was partially funded by the Bundesministerium fuer Bildung und
14  * Forschung (BMBF) of the Federal Republic of Germany (Foerderkennzeichen 01AK045).
15  * The authors alone are responsible for the contents.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21 
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  *
30  * Contact: discussion@sctp.de
31  *          dreibh@iem.uni-due.de
32  *          tuexen@fh-muenster.de
33  *
34  * Purpose: Synchronizable Implementation
35  *
36  */
37 
38 
39 #include "tdsystem.h"
40 #include "synchronizable.h"
41 #include "thread.h"
42 
43 
44 #include <pthread.h>
45 #include <signal.h>
46 
47 
48 
49 // ###### Constructor #######################################################
Synchronizable(const char * name,const bool recursive)50 Synchronizable::Synchronizable(const char* name, const bool recursive)
51 {
52    pthread_mutexattr_t mutexattr;
53    pthread_mutexattr_init(&mutexattr);
54    Recursive = recursive;
55 #ifndef NO_RECURSIVE_MUTEX
56    if(Recursive) {
57       // Initialize mutex for synchronization; mutextype has to be set to
58       // PTHREAD_MUTEX_RECURSIVE to allow nested calls of synchronized() and
59       // unsynchronized()!
60       pthread_mutexattr_settype(&mutexattr,PTHREAD_MUTEX_RECURSIVE);
61    }
62 #else
63    // Keep track of recursion, if recursive mutexes are not supported.
64    RecursionLevel = 0;
65    Owner          = 0;
66 #endif
67    pthread_mutex_init(&Mutex,&mutexattr);
68    pthread_mutexattr_destroy(&mutexattr);
69    setName(name);
70 }
71 
72 
73 // ###### Destructor ########################################################
~Synchronizable()74 Synchronizable::~Synchronizable()
75 {
76    pthread_mutex_destroy(&Mutex);
77 }
78 
79 
80 // ###### Reinitialize ######################################################
resynchronize()81 void Synchronizable::resynchronize()
82 {
83    pthread_mutex_destroy(&Mutex);
84    pthread_mutexattr_t mutexattr;
85    pthread_mutexattr_init(&mutexattr);
86 #ifndef NO_RECURSIVE_MUTEX
87    if(Recursive) {
88       pthread_mutexattr_settype(&mutexattr,PTHREAD_MUTEX_RECURSIVE);
89    }
90 #else
91    RecursionLevel = 0;
92    Owner          = 0;
93 #endif
94    pthread_mutex_init(&Mutex,&mutexattr);
95    pthread_mutexattr_destroy(&mutexattr);
96 }
97