Ticket #3883: pthread_attr_getstack_2.patch

File pthread_attr_getstack_2.patch, 5.6 KB (added by mjw, 16 years ago)
  • src/system/kernel/thread.cpp

     
    567567                status = thread->user_stack_area;
    568568                kill_thread(thread->id);
    569569            }
     570            attributes.stack_address = (void *)thread->user_stack_base;
    570571        } else {
    571572            thread->user_stack_base = (addr_t)attributes.stack_address;
    572573            thread->user_stack_size = attributes.stack_size;
     
    28042805    threadID = create_thread(attributes, false);
    28052806
    28062807    if (threadID >= 0)
     2808    {
    28072809        user_debug_thread_created(threadID);
     2810+       // Copy the attributes back to tell the user which stack_address we are
     2811+       // using.
     2812+       if (user_memcpy(userAttributes, &attributes, sizeof(*userAttributes)) < B_OK)
     2813+           dprintf("_user_spawn_thread: error copying attributes baack to user space\n");
     2814    }
    28082815
    28092816    return threadID;
    28102817}
  • src/system/libroot/posix/pthread/pthread.c

     
    2222static const pthread_attr pthread_attr_default = {
    2323    PTHREAD_CREATE_JOINABLE,
    2424    B_NORMAL_PRIORITY,
     25    NULL,
    2526    USER_STACK_SIZE
    2627};
    2728
     
    7576pthread_create(pthread_t *_thread, const pthread_attr_t *_attr,
    7677        void *(*startRoutine)(void*), void *arg)
    7778{
    78     const pthread_attr *attr = NULL;
    7979    pthread_thread *thread;
    8080    struct thread_creation_attributes attributes;
    8181
    8282    if (_thread == NULL)
    8383        return B_BAD_VALUE;
    8484
    85     if (_attr == NULL)
    86         attr = &pthread_attr_default;
    87     else
    88         attr = *_attr;
    89 
    9085    thread = (pthread_thread *)malloc(sizeof(pthread_thread));
    9186    if (thread == NULL)
    9287        return B_WOULD_BLOCK;
    9388
     89    if (_attr == NULL)
     90        memcpy(&thread->attr, &pthread_attr_default, sizeof(thread->attr));
     91    else
     92        memcpy(&thread->attr, *_attr, sizeof(thread->attr));
     93
    9494    thread->entry = startRoutine;
    9595    thread->entry_argument = arg;
    9696    thread->exit_value = NULL;
     
    100100    thread->cleanup_handlers = NULL;
    101101    thread->flags = 0;
    102102
    103     if (attr->detach_state == PTHREAD_CREATE_DETACHED)
     103    if (thread->attr.detach_state == PTHREAD_CREATE_DETACHED)
    104104        thread->flags |= THREAD_DETACHED;
    105105
    106106    if (sPthreadSlot == -1) {
     
    110110
    111111    attributes.entry = pthread_thread_entry;
    112112    attributes.name = "pthread func";
    113     attributes.priority = attr->sched_priority;
     113    attributes.priority = thread->attr.sched_priority;
    114114    attributes.args1 = NULL;
    115115    attributes.args2 = thread;
    116116    attributes.stack_address = NULL;
    117     attributes.stack_size = attr->stack_size;
     117    attributes.stack_size = thread->attr.stack_size;
    118118
    119119    thread->id = _kern_spawn_thread(&attributes);
    120120    if (thread->id < 0) {
     
    123123        return B_WOULD_BLOCK;
    124124    }
    125125
     126    thread->attr.stack_address = attributes.stack_address;
     127
    126128    resume_thread(thread->id);
    127129    *_thread = thread;
    128130
  • src/system/libroot/posix/pthread/pthread_attr.c

     
    1515
    1616
    1717int
     18pthread_getattr_np(pthread_t _thread, pthread_attr_t *_attr)
     19{
     20    pthread_thread *thread = (pthread_thread *)_thread;
     21    pthread_attr *attr;
     22
     23    if (thread == NULL || _attr == NULL)
     24        return B_BAD_VALUE;
     25
     26    attr = (pthread_attr *)malloc(sizeof(pthread_attr));
     27    if (attr == NULL)
     28        return B_NO_MEMORY;
     29
     30    memcpy(_attr, &thread->attr, sizeof(pthread_attr_t));
     31
     32    *_attr = attr;
     33
     34    return B_OK;
     35}
     36
     37int
    1838pthread_attr_init(pthread_attr_t *_attr)
    1939{
    2040    pthread_attr *attr;
     
    2848
    2949    attr->detach_state = PTHREAD_CREATE_JOINABLE;
    3050    attr->sched_priority = B_NORMAL_PRIORITY;
     51    attr->stack_address = NULL;
    3152    attr->stack_size = USER_STACK_SIZE;
    3253
    3354    *_attr = attr;
     
    134155
    135156    return 0;
    136157}
     158
     159int
     160pthread_attr_getstack(const pthread_attr_t *_attr, void **stackaddr, size_t *stacksize)
     161{
     162    pthread_attr *attr = (pthread_attr *)_attr;
     163
     164    if (attr == NULL)
     165        return EINVAL;
     166
     167    *stackaddr = attr->stack_address;
     168    *stacksize = attr->stack_size;
     169
     170    return 0;
     171}
     172
  • src/system/libroot/posix/pthread/pthread_private.h

     
    2828typedef struct _pthread_attr {
    2929    int32       detach_state;
    3030    int32       sched_priority;
     31    void        *stack_address;
    3132    size_t      stack_size;
    3233} pthread_attr;
    3334
     
    6061    bool        cancelled;
    6162    struct pthread_key_data specific[PTHREAD_KEYS_MAX];
    6263    struct __pthread_cleanup_handler *cleanup_handlers;
     64    pthread_attr attr;
    6365    // TODO: move pthread keys in here, too
    6466} pthread_thread;
    6567
  • headers/posix/pthread.h

     
    229229    int *contentionScope);
    230230extern int pthread_attr_setscope(pthread_attr_t *attr, int contentionScope);
    231231
     232extern int pthread_getattr_np(pthread_t _thread, pthread_attr_t *_attr);
     233
     234/* [TSA TSS] */
     235extern int pthread_attr_getstack(const pthread_attr_t *attr,
     236    void **stackaddr, size_t *stacksize);
     237
    232238#if 0   /* Unimplemented attribute functions: */
    233239
    234240/* mandatory! */
     
    257263extern int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr);
    258264
    259265/* [TSA TSS] */
    260 extern int pthread_attr_getstack(const pthread_attr_t *attr,
    261     void **stackaddr, size_t *stacksize);
    262266extern int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize);
    263267
    264268#endif  /* 0 */