Example #1
0
__attribute_cold__
__attribute_noinline__
static int
plasma_spin_nprocs_init (void)
{
    long nprocs_onln = plasma_sysconf_nprocessors_onln();
    uint32_t nshift_onln;
    uint32_t nshift_accum = 0;

    if (nprocs_onln < 1)
        nprocs_onln = 1;

    nshift_onln = (uint32_t)nprocs_onln;
    while ((nshift_onln >>= 1))
        ++nshift_accum;
    nshift = nshift_accum;  /* power of 2 of available CPUs (0 if only 1 CPU) */
    plasma_membar_StoreStore();
    return (nprocs = (uint32_t)nprocs_onln);

    /* store ordering above is not critical; so our readers below do not acquire
     * If there is a race between threads before nprocs is set, then if a thread
     * reads nshift = 0, but nprocs != 0, then thread might be slightly slower
     * acquiring the spin lock the first time.  Not a big deal.  Likewise, it
     * is not problem if multiple threads run this routine since this routine
     * stores results atomically (via int-sized natural alignment) to the
     * static variables and the results of initialization are the same. */
}
Example #2
0
__attribute_noinline__
static bool
_nss_mcdb_db_openshared(const enum nss_dbtype dbtype)
{
#ifdef _THREAD_SAFE
    static pthread_mutex_t _nss_mcdb_global_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
    struct mcdb_mmap * const restrict map = &_nss_mcdb_mmap_st[dbtype];
    bool rc;

    if (pthread_mutex_lock(&_nss_mcdb_global_mutex) != 0)
        return false;
    if (__builtin_expect( _nss_mcdb_mmap[dbtype] != 0, false)) {
        /* init'ed by another thread while waiting for mutex */
        pthread_mutex_unlock(&_nss_mcdb_global_mutex);
        return true;
    }

#ifdef _FORTIFY_SOURCE
    {   static bool atexit_once = true;
        if (atexit_once) {
            atexit_once = false;
            atexit(_nss_mcdb_atexit);
        }
    }
#endif

    /* pass full path in fname instead of separate dirname and basename
     * (not using openat(), fstatat() where someone might close dfd on us)
     * use static storage for initial struct mcdb_mmap for each dbtype
     * to avoid malloc allocation in short-lived programs, and to maintain
     * a reference to maps to keep them available. */
    if ((rc = (NULL != mcdb_mmap_create_h(map, NULL, _nss_dbnames[dbtype],
                                          malloc, free)))) {
        plasma_membar_StoreStore();
        _nss_mcdb_mmap[dbtype] = map;
    }

    pthread_mutex_unlock(&_nss_mcdb_global_mutex);
    return rc;
}