~ubuntu-branches/ubuntu/saucy/mutextrace/saucy-proposed

« back to all changes in this revision

Viewing changes to thread_create.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon Richter
  • Date: 2009-11-30 15:26:21 UTC
  • Revision ID: james.westby@ubuntu.com-20091130152621-kxucetq7b43lx9fu
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifdef HAVE_CONFIG_H
 
2
#include <config.h>
 
3
#endif
 
4
 
 
5
#include "store.h"
 
6
#include "real.h"
 
7
 
 
8
#include <stdio.h>
 
9
 
 
10
struct startupinfo
 
11
{
 
12
    void *(*start_routine)(void *);
 
13
    void *arg;
 
14
    pthread_cond_t cond;
 
15
    pthread_mutex_t mutex;
 
16
};
 
17
 
 
18
static void cleanup(void *thread)
 
19
{
 
20
    struct thread *t = (struct thread *)thread;
 
21
 
 
22
    fprintf(stderr, "thread exit #%u\n", t->num);
 
23
}
 
24
 
 
25
static void *startup(void *startupinfo)
 
26
{
 
27
    struct startupinfo *sui = (struct startupinfo *)startupinfo;
 
28
 
 
29
    void *(*start_routine)(void *) = sui->start_routine;
 
30
    void *arg = sui->arg;
 
31
    
 
32
    real_mutex_lock(&sui->mutex);
 
33
    real_cond_signal(&sui->cond);
 
34
    real_mutex_unlock(&sui->mutex);
 
35
 
 
36
    struct thread *t = find_thread(pthread_self());
 
37
 
 
38
    fprintf(stderr, "thread create #%u\n", t->num);
 
39
 
 
40
    void *res;
 
41
 
 
42
    pthread_cleanup_push(&cleanup, t);
 
43
 
 
44
    res = start_routine(arg);
 
45
 
 
46
    pthread_cleanup_pop(1);
 
47
 
 
48
    return res;
 
49
}
 
50
 
 
51
int pthread_create(
 
52
        pthread_t *thread,
 
53
        pthread_attr_t const *attr,
 
54
        void *(*start_routine)(void*),
 
55
        void *arg)
 
56
{
 
57
    struct startupinfo sui = { start_routine, arg, {}, {} };
 
58
    real_cond_init(&sui.cond, 0);
 
59
    real_mutex_init(&sui.mutex, 0);
 
60
 
 
61
    real_mutex_lock(&sui.mutex);
 
62
 
 
63
    int res = real_create(thread, attr, &startup, &sui);
 
64
 
 
65
    real_cond_wait(&sui.cond, &sui.mutex);
 
66
 
 
67
    real_mutex_unlock(&sui.mutex);
 
68
 
 
69
    return res;
 
70
}