Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d10d829

Browse files
Added C++ support in thread.h; don't use signals if not strictly
necessary, and when they are, use SIGKILL; when compiled with -DDEBUG, only print debug messages when "THREADDEBUG" is set in the environment.
1 parent 0aead9f commit d10d829

3 files changed

Lines changed: 40 additions & 15 deletions

File tree

Include/pythread.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
#ifndef _THREAD_H_included
22
#define _THREAD_H_included
33

4-
#ifdef __STDC__
4+
#if defined(__STDC__) || defined(__cplusplus)
55
#define _P(args) args
66
#else
77
#define _P(args) ()
88
#endif
99

10+
typedef void *type_lock;
11+
typedef void *type_sema;
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
1017
void init_thread _P((void));
1118
int start_new_thread _P((void (*)(void *), void *));
1219
void exit_thread _P((void));
1320
void _exit_thread _P((void));
1421

15-
typedef void *type_lock;
16-
1722
type_lock allocate_lock _P((void));
1823
void free_lock _P((type_lock));
1924
int acquire_lock _P((type_lock, int));
2025
#define WAIT_LOCK 1
2126
#define NOWAIT_LOCK 0
2227
void release_lock _P((type_lock));
2328

24-
typedef void *type_sema;
25-
2629
type_sema allocate_sema _P((int));
2730
void free_sema _P((type_sema));
2831
void down_sema _P((type_sema));
@@ -31,6 +34,10 @@ void up_sema _P((type_sema));
3134
void exit_prog _P((int));
3235
void _exit_prog _P((int));
3336

37+
#ifdef __cplusplus
38+
}
39+
#endif
40+
3441
#undef _P
3542

3643
#endif

Include/thread.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
#ifndef _THREAD_H_included
22
#define _THREAD_H_included
33

4-
#ifdef __STDC__
4+
#if defined(__STDC__) || defined(__cplusplus)
55
#define _P(args) args
66
#else
77
#define _P(args) ()
88
#endif
99

10+
typedef void *type_lock;
11+
typedef void *type_sema;
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
1017
void init_thread _P((void));
1118
int start_new_thread _P((void (*)(void *), void *));
1219
void exit_thread _P((void));
1320
void _exit_thread _P((void));
1421

15-
typedef void *type_lock;
16-
1722
type_lock allocate_lock _P((void));
1823
void free_lock _P((type_lock));
1924
int acquire_lock _P((type_lock, int));
2025
#define WAIT_LOCK 1
2126
#define NOWAIT_LOCK 0
2227
void release_lock _P((type_lock));
2328

24-
typedef void *type_sema;
25-
2629
type_sema allocate_sema _P((int));
2730
void free_sema _P((type_sema));
2831
void down_sema _P((type_sema));
@@ -31,6 +34,10 @@ void up_sema _P((type_sema));
3134
void exit_prog _P((int));
3235
void _exit_prog _P((int));
3336

37+
#ifdef __cplusplus
38+
}
39+
#endif
40+
3441
#undef _P
3542

3643
#endif

Python/thread.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "thread.h"
22

33
#ifdef DEBUG
4-
#define dprintf(args) printf args
4+
static int thread_debug = 0;
5+
#define dprintf(args) (thread_debug && printf args)
56
#else
67
#define dprintf(args)
78
#endif
@@ -102,6 +103,9 @@ void init_thread _P0()
102103
struct sigaction s;
103104
#endif
104105

106+
#ifdef DEBUG
107+
thread_debug = getenv("THREADDEBUG") != 0;
108+
#endif
105109
dprintf(("init_thread called\n"));
106110
if (initialized)
107111
return;
@@ -112,7 +116,7 @@ void init_thread _P0()
112116
atexit(maybe_exit);
113117
s.sa_handler = exit_sig;
114118
sigemptyset(&s.sa_mask);
115-
sigaddset(&s.sa_mask, SIGUSR1);
119+
/*sigaddset(&s.sa_mask, SIGUSR1);*/
116120
s.sa_flags = 0;
117121
sigaction(SIGUSR1, &s, 0);
118122
prctl(PR_SETEXITSIG, SIGUSR1);
@@ -189,8 +193,12 @@ static void do_exit_thread _P1(no_cleanup, int no_cleanup)
189193
int i;
190194

191195
/* notify other threads */
192-
for (i = 0; i < maxpidindex; i++)
193-
(void) kill(pidlist[i], SIGUSR1);
196+
if (nthreads >= 0) {
197+
dprintf(("kill other threads\n"));
198+
for (i = 0; i < maxpidindex; i++)
199+
(void) kill(pidlist[i], SIGKILL);
200+
_exit(exit_status);
201+
}
194202
}
195203
waiting_for_threads = 1;
196204
ussetlock(wait_lock);
@@ -212,7 +220,8 @@ static void do_exit_thread _P1(no_cleanup, int no_cleanup)
212220
if (waiting_for_threads) {
213221
dprintf(("main thread is waiting\n"));
214222
usunsetlock(wait_lock);
215-
}
223+
} else if (do_exit)
224+
(void) kill(my_pid, SIGUSR1);
216225
(void) usunsetlock(count_lock);
217226
_exit(0);
218227
#endif
@@ -361,6 +370,8 @@ type_sema allocate_sema _P1(value, int value)
361370
#endif
362371

363372
dprintf(("allocate_sema called\n"));
373+
if (!initialized)
374+
init_thread();
364375

365376
#ifdef __sgi
366377
sema = usnewsema(shared_arena, value);

0 commit comments

Comments
 (0)