Skip to content

Commit 6c4e55a

Browse files
aldrik-ramaekersSiegeLord
authored andcommitted
define stacksize for new threads
1 parent b17c338 commit 6c4e55a

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

include/allegro5/internal/aintern_thread.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typedef struct _AL_COND _AL_COND;
1414

1515

1616
void _al_thread_create(_AL_THREAD*, void (*proc)(_AL_THREAD*, void*), void *arg);
17+
void _al_thread_with_stacksize_create(_AL_THREAD*, void (*proc)(_AL_THREAD*, void*), void *arg, size_t stacksize);
1718
void _al_thread_set_should_stop(_AL_THREAD *);
1819
/* static inline bool _al_get_thread_should_stop(_AL_THREAD *); */
1920
void _al_thread_join(_AL_THREAD*);

include/allegro5/threads.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ typedef struct ALLEGRO_COND ALLEGRO_COND;
3737

3838
AL_FUNC(ALLEGRO_THREAD *, al_create_thread,
3939
(void *(*proc)(ALLEGRO_THREAD *thread, void *arg), void *arg));
40+
AL_FUNC(ALLEGRO_THREAD *, al_create_thread_with_stacksize,
41+
(void *(*proc)(ALLEGRO_THREAD *thread, void *arg), void *arg, size_t stacksize));
4042
AL_FUNC(void, al_start_thread, (ALLEGRO_THREAD *outer));
4143
AL_FUNC(void, al_join_thread, (ALLEGRO_THREAD *outer, void **ret_value));
4244
AL_FUNC(void, al_set_thread_should_stop, (ALLEGRO_THREAD *outer));

src/threads.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,21 @@ ALLEGRO_THREAD *al_create_thread(
126126
return outer;
127127
}
128128

129-
129+
/* Function: al_create_thread_with_stacksize
130+
*/
131+
ALLEGRO_THREAD *al_create_thread_with_stacksize(
132+
void *(*proc)(ALLEGRO_THREAD *thread, void *arg), void *arg, size_t stacksize)
133+
{
134+
ALLEGRO_THREAD *outer = create_thread();
135+
outer->thread_state = THREAD_STATE_CREATED;
136+
_al_mutex_init(&outer->mutex);
137+
_al_cond_init(&outer->cond);
138+
outer->arg = arg;
139+
outer->proc = proc;
140+
_al_thread_with_stacksize_create(&outer->thread, thread_func_trampoline, outer, stacksize);
141+
/* XXX _al_thread_create should return an error code */
142+
return outer;
143+
}
130144

131145
/* Function: al_run_detached_thread
132146
*/

src/unix/uxthread.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,34 @@ void _al_thread_create(_AL_THREAD *thread, void (*proc)(_AL_THREAD*, void*), voi
7070
}
7171

7272

73+
void _al_thread_with_stacksize_create(_AL_THREAD* thread, void (*proc)(_AL_THREAD*, void*), void *arg, size_t stacksize)
74+
{
75+
ASSERT(thread);
76+
ASSERT(proc);
77+
{
78+
int status;
79+
80+
pthread_mutex_init(&thread->mutex, NULL);
81+
82+
thread->should_stop = false;
83+
thread->proc = proc;
84+
thread->arg = arg;
85+
86+
// stacksize should be PTHREAD_STACK_MIN or more (16kb?)
87+
pthread_attr_t thread_attr;
88+
int s = 0;
89+
s = pthread_attr_init(&thread_attr);
90+
ASSERT(s==0);
91+
s = pthread_attr_setstacksize(&thread_attr, stacksize);
92+
93+
status = pthread_create(&thread->thread, &thread_attr, thread_proc_trampoline, thread);
94+
ASSERT(status == 0);
95+
if (status != 0)
96+
abort();
97+
}
98+
}
99+
100+
73101
void _al_thread_set_should_stop(_AL_THREAD *thread)
74102
{
75103
ASSERT(thread);

src/win/wxthread.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ void _al_thread_create(_AL_THREAD *thread, void (*proc)(_AL_THREAD*, void*), voi
5757
}
5858

5959

60+
void _al_thread_with_stacksize_create(_AL_THREAD* thread, void (*proc)(_AL_THREAD*, void*), void *arg, size_t stacksize)
61+
{
62+
ASSERT(thread);
63+
ASSERT(proc);
64+
{
65+
InitializeCriticalSection(&thread->cs);
66+
67+
thread->should_stop = false;
68+
thread->proc = proc;
69+
thread->arg = arg;
70+
71+
thread->thread = (void *)_beginthreadex(NULL, stacksize,
72+
thread_proc_trampoline, thread, 0, NULL);
73+
}
74+
}
75+
76+
6077
void _al_thread_set_should_stop(_AL_THREAD *thread)
6178
{
6279
ASSERT(thread);

0 commit comments

Comments
 (0)