~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to thread_win32.ci

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
  thread_win32.ci -
5
5
 
6
 
  $Author: nobu $
7
 
  $Date: 2007-06-05 13:30:38 +0900 (火, 05  6月 2007) $
 
6
  $Author: ko1 $
 
7
  $Date: 2007-08-28 01:48:14 +0900 (火, 28  8月 2007) $
8
8
 
9
9
  Copyright (C) 2004-2006 Koichi Sasada
10
10
 
20
20
#define native_thread_yield() Sleep(0)
21
21
#define remove_signal_thread_list(th)
22
22
 
 
23
static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
 
24
 
 
25
static rb_thread_t *
 
26
ruby_thread_from_native(void)
 
27
{
 
28
    return TlsGetValue(ruby_native_thread_key);
 
29
}
 
30
 
 
31
static int
 
32
ruby_thread_set_native(rb_thread_t *th)
 
33
{
 
34
    return TlsSetValue(ruby_native_thread_key, th);
 
35
}
 
36
 
23
37
static void
24
 
Init_native_thread()
 
38
Init_native_thread(void)
25
39
{
26
40
    rb_thread_t *th = GET_THREAD();
 
41
 
 
42
    ruby_native_thread_key = TlsAlloc();
27
43
    DuplicateHandle(GetCurrentProcess(),
28
44
                    GetCurrentThread(),
29
45
                    GetCurrentProcess(),
106
122
    return ret;
107
123
}
108
124
 
109
 
static void ubf_handle(rb_thread_t *th);
 
125
static void ubf_handle(rb_thread_t *th, void *ptr);
110
126
#define ubf_select ubf_handle
111
127
 
112
128
int
120
136
{
121
137
    int ret;
122
138
 
123
 
    BLOCKING_REGION(ret = rb_w32_wait_events_blocking(events, num, timeout), ubf_handle);
 
139
    BLOCKING_REGION(ret = rb_w32_wait_events_blocking(events, num, timeout), ubf_handle, 0);
124
140
    return ret;
125
141
}
126
142
 
140
156
    }
141
157
}
142
158
 
 
159
#ifdef _MSC_VER
 
160
#define HAVE__BEGINTHREADEX 1
 
161
#else
 
162
#undef HAVE__BEGINTHREADEX
 
163
#endif
 
164
 
 
165
#ifdef HAVE__BEGINTHREADEX
 
166
#define start_thread (HANDLE)_beginthreadex
 
167
typedef unsigned long (_stdcall *w32_thread_start_func)(void*);
 
168
#else
 
169
#define start_thread CreateThread
 
170
typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
 
171
#endif
 
172
 
143
173
static HANDLE
144
 
w32_create_thread(DWORD stack_size, void *func, void *val)
 
174
w32_create_thread(DWORD stack_size, w32_thread_start_func func, void *val)
145
175
{
146
 
    return (HANDLE)_beginthreadex(0, stack_size, func, val, CREATE_SUSPENDED, 0);
 
176
    return start_thread(0, stack_size, func, val, CREATE_SUSPENDED, 0);
147
177
}
148
178
 
149
179
int
157
187
{
158
188
    int ret;
159
189
 
160
 
    BLOCKING_REGION(ret = rb_w32_sleep(msec), ubf_handle);
 
190
    BLOCKING_REGION(ret = rb_w32_sleep(msec), ubf_handle, 0);
161
191
    return ret;
162
192
}
163
193
 
279
309
#endif
280
310
}
281
311
 
282
 
 
283
 
NOINLINE(static int
284
 
         thread_start_func_2(rb_thread_t *th, VALUE *stack_start));
 
312
struct cond_event_entry {
 
313
    struct cond_event_entry* next;
 
314
    HANDLE event;
 
315
};
 
316
 
 
317
struct rb_thread_cond_struct {
 
318
    struct cond_event_entry *next;
 
319
    struct cond_event_entry *last;
 
320
};
 
321
 
 
322
void
 
323
native_cond_signal(rb_thread_cond_t *cond)
 
324
{
 
325
    /* cond is guarded by mutex */
 
326
    struct cond_event_entry *e = cond->next;
 
327
 
 
328
    if (e) {
 
329
        cond->next = e->next;
 
330
        SetEvent(e->event);
 
331
    }
 
332
    else {
 
333
        rb_bug("native_cond_signal: no pending threads");
 
334
    }
 
335
}
 
336
 
 
337
void
 
338
native_cond_broadcast(rb_thread_cond_t *cond)
 
339
{
 
340
    /* cond is guarded by mutex */
 
341
    struct cond_event_entry *e = cond->next;
 
342
    cond->next = 0;
 
343
 
 
344
    while (e) {
 
345
        SetEvent(e->event);
 
346
        e = e->next;
 
347
    }
 
348
}
 
349
 
 
350
void
 
351
native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex)
 
352
{
 
353
    DWORD r;
 
354
    struct cond_event_entry entry;
 
355
 
 
356
    entry.next = 0;
 
357
    entry.event = CreateEvent(0, FALSE, FALSE, 0);
 
358
 
 
359
    /* cond is guarded by mutex */
 
360
    if (cond->next) {
 
361
        cond->last->next = &entry;
 
362
        cond->last = &entry;
 
363
    }
 
364
    else {
 
365
        cond->next = &entry;
 
366
        cond->last = &entry;
 
367
    }
 
368
 
 
369
    native_mutex_unlock(mutex);
 
370
    {
 
371
        r = WaitForSingleObject(entry.event, INFINITE);
 
372
        if (r != WAIT_OBJECT_0) {
 
373
            rb_bug("native_cond_wait: WaitForSingleObject returns %d", r);
 
374
        }
 
375
    }
 
376
    native_mutex_lock(mutex);
 
377
 
 
378
    w32_close_handle(entry.event);
 
379
}
 
380
 
 
381
void
 
382
native_cond_initialize(rb_thread_cond_t *cond)
 
383
{
 
384
    cond->next = 0;
 
385
    cond->last = 0;
 
386
}
 
387
 
 
388
void
 
389
native_cond_destroy(rb_thread_cond_t *cond)
 
390
{
 
391
    /* */
 
392
}
285
393
 
286
394
static void
287
395
native_thread_destroy(rb_thread_t *th)
292
400
    w32_close_handle(intr);
293
401
}
294
402
 
295
 
static unsigned int _stdcall
 
403
static unsigned long _stdcall
296
404
thread_start_func_1(void *th_ptr)
297
405
{
298
406
    rb_thread_t *th = th_ptr;
304
412
    /* run */
305
413
    thread_debug("thread created (th: %p, thid: %p, event: %p)\n", th,
306
414
                 th->thread_id, th->native_thread_data.interrupt_event);
307
 
    thread_start_func_2(th, &stack_start);
 
415
    thread_start_func_2(th, &stack_start, 0);
308
416
 
309
417
    w32_close_handle(thread_id);
310
418
    thread_debug("thread deleted (th: %p)\n", th);
357
465
}
358
466
 
359
467
static void
360
 
ubf_handle(rb_thread_t *th)
 
468
ubf_handle(rb_thread_t *th, void *ptr)
361
469
{
362
470
    thread_debug("ubf_handle: %p\n", th);
363
471
    w32_set_event(th->native_thread_data.interrupt_event);
367
475
 
368
476
static HANDLE timer_thread_id = 0;
369
477
 
370
 
static unsigned int _stdcall
 
478
static unsigned long _stdcall
371
479
timer_thread_func(void *dummy)
372
480
{
373
481
    thread_debug("timer_thread\n");