Changeset 9a3b469 in mainline


Ignore:
Timestamp:
2012-12-04T04:09:08Z (12 years ago)
Author:
Adam Hraska <adam.hraska+hos@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d54b303
Parents:
1b7eec9
Message:

malloc avoids using futexes during initialization of the main thread of a program.

Location:
uspace/lib/c/generic
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/libc.c

    r1b7eec9 r9a3b469  
    5151#include "private/malloc.h"
    5252#include "private/io.h"
     53#include "private/thread.h"
    5354
    5455#ifdef CONFIG_RTLD
     
    7273        /* Save the PCB pointer */
    7374        __pcb = (pcb_t *) pcb_ptr;
     75       
     76        atomic_inc(&_created_thread_cnt);
    7477       
    7578        /* The basic run-time environment is setup */
  • uspace/lib/c/generic/malloc.c

    r1b7eec9 r9a3b469  
    4747#include <adt/gcdlcm.h>
    4848#include "private/malloc.h"
     49#include "private/thread.h"
    4950
    5051/** Magic used in heap headers. */
     
    785786void *malloc(const size_t size)
    786787{
    787         futex_down(&malloc_futex);
    788         void *block = malloc_internal(size, BASE_ALIGN);
    789         futex_up(&malloc_futex);
    790        
    791         return block;
     788        /* Do not use futexes for allocations during main thread initialization. */
     789        if (0 == atomic_get(&_created_thread_cnt)) {
     790                return malloc_internal(size, BASE_ALIGN);
     791        } else {
     792                futex_down(&malloc_futex);
     793                void *block = malloc_internal(size, BASE_ALIGN);
     794                futex_up(&malloc_futex);
     795                return block;
     796        }
    792797}
    793798
     
    807812        size_t palign =
    808813            1 << (fnzb(max(sizeof(void *), align) - 1) + 1);
    809        
    810         futex_down(&malloc_futex);
    811         void *block = malloc_internal(size, palign);
    812         futex_up(&malloc_futex);
    813        
    814         return block;
     814
     815        /* Do not use futexes for allocations during main thread initialization. */
     816        if (0 == atomic_get(&_created_thread_cnt)) {
     817                return malloc_internal(size, palign);
     818        } else {
     819                futex_down(&malloc_futex);
     820                void *block = malloc_internal(size, palign);
     821                futex_up(&malloc_futex);
     822
     823                return block;
     824        }
    815825}
    816826
  • uspace/lib/c/generic/private/thread.h

    r1b7eec9 r9a3b469  
    4141extern void __thread_main(uspace_arg_t *);
    4242
     43struct atomic;
     44extern struct atomic _created_thread_cnt;
     45
    4346#endif
    4447
  • uspace/lib/c/generic/thread.c

    r1b7eec9 r9a3b469  
    4646#include "private/thread.h"
    4747
     48/**
     49 * The number of threads that have been created and initialized since
     50 * the start of the program.
     51 */
     52atomic_t _created_thread_cnt = {0};
     53
    4854/** Main thread function.
    4955 *
     
    6268       
    6369        __tcb_set(fibril->tcb);
     70       
     71        atomic_inc(&_created_thread_cnt);
    6472       
    6573        uarg->uspace_thread_function(uarg->uspace_thread_arg);
Note: See TracChangeset for help on using the changeset viewer.