美文网首页
pthread 函数解释

pthread 函数解释

作者: 萍水相逢_程序员 | 来源:发表于2019-07-18 16:06 被阅读0次
    #ifndef _PTHREAD_H_
    #define _PTHREAD_H_
    
    #include <limits.h>
    #include <bits/pthread_types.h>
    #include <sched.h>
    #include <sys/cdefs.h>
    #include <sys/types.h>
    #include <time.h>
    
    __BEGIN_DECLS
    
    enum {
        PTHREAD_MUTEX_NORMAL = 0,
        PTHREAD_MUTEX_RECURSIVE = 1,
        PTHREAD_MUTEX_ERRORCHECK = 2,
    
        PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
        PTHREAD_MUTEX_RECURSIVE_NP  = PTHREAD_MUTEX_RECURSIVE,
    
        PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
    };
    
    #define PTHREAD_MUTEX_INITIALIZER { { ((PTHREAD_MUTEX_NORMAL & 3) << 14) } }
    #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP { { ((PTHREAD_MUTEX_RECURSIVE & 3) << 14) } }
    #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP { { ((PTHREAD_MUTEX_ERRORCHECK & 3) << 14) } }
    
    #define PTHREAD_COND_INITIALIZER  { { 0 } }
    
    #define PTHREAD_RWLOCK_INITIALIZER  { { 0 } }
    
    enum {
        PTHREAD_RWLOCK_PREFER_READER_NP = 0,
        PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP = 1,
    };
    
    #define PTHREAD_ONCE_INIT 0
    
    #if __ANDROID_API__ >= __ANDROID_API_N__
    #define PTHREAD_BARRIER_SERIAL_THREAD -1
    #endif
    
    #if defined(__LP64__)
    #define PTHREAD_STACK_MIN (4 * PAGE_SIZE)
    #else
    #define PTHREAD_STACK_MIN (2 * PAGE_SIZE)
    #endif
    
    #define PTHREAD_CREATE_DETACHED  0x00000001
    #define PTHREAD_CREATE_JOINABLE  0x00000000
    
    #define PTHREAD_PROCESS_PRIVATE  0
    #define PTHREAD_PROCESS_SHARED   1
    
    #define PTHREAD_SCOPE_SYSTEM     0
    #define PTHREAD_SCOPE_PROCESS    1
    
    
    #if __ANDROID_API__ >= 12
    int pthread_atfork(void (*__prepare)(void), void (*__parent)(void), void (*__child)(void)) __INTRODUCED_IN(12);
    #endif /* __ANDROID_API__ >= 12 */
    
    
    //删除线程属性
    int pthread_attr_destroy(pthread_attr_t* __attr);
    //以下是获取线程相关的属性
    //线程的分离状态
    int pthread_attr_getdetachstate(const pthread_attr_t* __attr, int* __state);
    // 线程栈末尾的警戒缓冲区大小
    int pthread_attr_getguardsize(const pthread_attr_t* __attr, size_t* __size);
    // 线程的调度参数
    int pthread_attr_getschedparam(const pthread_attr_t* __attr, struct sched_param* __param);
    //线程调度策略
    int pthread_attr_getschedpolicy(const pthread_attr_t* __attr, int* __policy);
    // 线程的作用域
    int pthread_attr_getscope(const pthread_attr_t* __attr, int* __scope);
    //线程栈的位置
    int pthread_attr_getstack(const pthread_attr_t* __attr, void** __addr, size_t* __size);
    // 线程栈的大小
    int pthread_attr_getstacksize(const pthread_attr_t* __attr, size_t* __size);
    //初始化线程属性
    int pthread_attr_init(pthread_attr_t* __attr);
    //设置线程相关属性
    int pthread_attr_setdetachstate(pthread_attr_t* __attr, int __state);
    int pthread_attr_setguardsize(pthread_attr_t* __attr, size_t __size);
    int pthread_attr_setschedparam(pthread_attr_t* __attr, const struct sched_param* __param);
    int pthread_attr_setschedpolicy(pthread_attr_t* __attr, int __policy);
    int pthread_attr_setscope(pthread_attr_t* __attr, int __scope);
    int pthread_attr_setstack(pthread_attr_t* __attr, void* __addr, size_t __size);
    int pthread_attr_setstacksize(pthread_attr_t* __addr, size_t __size);
    
    //条件变量 销毁
    int pthread_condattr_destroy(pthread_condattr_t* __attr);
    
    #if __ANDROID_API__ >= 21
    int pthread_condattr_getclock(const pthread_condattr_t* __attr, clockid_t* __clock) __INTRODUCED_IN(21);
    #endif /* __ANDROID_API__ >= 21 */
    
    int pthread_condattr_getpshared(const pthread_condattr_t* __attr, int* __shared)
    //条件变量初始化
    int pthread_condattr_init(pthread_condattr_t* __attr);
    
    #if __ANDROID_API__ >= 21
    int pthread_condattr_setclock(pthread_condattr_t* __attr, clockid_t __clock) __INTRODUCED_IN(21);
    #endif /* __ANDROID_API__ >= 21 */
    
    int pthread_condattr_setpshared(pthread_condattr_t* __attr, int __shared);
    //多个线程处于堵塞等待状态,必须使用此函数唤醒
    int pthread_cond_broadcast(pthread_cond_t* __cond);
    //销毁
    int pthread_cond_destroy(pthread_cond_t* __cond);
    //初始化
    int pthread_cond_init(pthread_cond_t* __cond, const pthread_condattr_t* __attr);
    // 发信号 给正在等待条件变量的另一个线程
    int pthread_cond_signal(pthread_cond_t* __cond);
    //阻塞调用线程
    int pthread_cond_timedwait(pthread_cond_t* __cond, pthread_mutex_t* __mutex, const struct timespec* __timeout);
    int pthread_cond_wait(pthread_cond_t* __cond, pthread_mutex_t* __mutex);
    
    /**
     *
     * @param __pthread_ptr  新线程的标识符,为一个long
     * @param __attr   设置新线程的属性,传递NULL表示设置为默认线程属性
     * @param __start_routine  指定新线程运行时函数  start_routine返回时,这个线程就退出了
     * @return  成功返回0, 失败返回错误号
     */
    int pthread_create(pthread_t* __pthread_ptr, pthread_attr_t const* __attr, void* (*__start_routine)(void*), void*);
    
    
    //1.在任何一个时间点上,线程是可结合的(joinable)或者是分离的(detached)。
    //
    //2.一个可结合的线程能够被其他线程收回其资源和杀死。在被其他线程回收之前,它的存储器资源
    //(例如栈)是不释放的。(默认情况下线程的创建都是可结合的)
    //
    //3.一个分离的线程是不能被其他线程回收或杀死的,它的存储器 资源在它终止时由系统自动释放。
    //
    //4. 如果一个可结合线程结束运行但没有被join,会导致部分资源没有被回收,
    // 所以创建线程者应该调用pthread_join来等待线程运行结束,并可得到线程的退出代码,回收其资源。
    
    /**
     *  非阻塞 立即返回  这将该子线程的状态设置为分离的(detached)
     *  ,该线程运行结束后会自动释放所有资源。
     * @param __pthread
     * @return
     */
    int pthread_detach(pthread_t __pthread);
    
    // 线程终止自己
    /**
     * @param __return_value  是void *类型,其它线程可以调用pthread_join获得这个指针
     * 通过retval参数向线程的回收者传递其退出信息。它执行之后不会返回到调用者,且永远不会失败。
     * 需要注意,pthread_exit或者return返回的指针所指向的内存单元必须是全局的或者是由malloc分 配的,
     * 不能在线程函数的栈上分配,因为当其它线程得到这个返回指针时线程函数已经退出了。
     */
    void pthread_exit(void* __return_value) __noreturn;
    
    // 用于比较两个pthread_t是否相等
    int pthread_equal(pthread_t __lhs, pthread_t __rhs);
    
    int pthread_getattr_np(pthread_t __pthread, pthread_attr_t* __attr);
    
    int pthread_getcpuclockid(pthread_t __pthread, clockid_t* __clock);
    
    int pthread_getschedparam(pthread_t __pthread, int* __policy, struct sched_param* __param);
    
    void* pthread_getspecific(pthread_key_t __key);
    
    
    #if __ANDROID_API__ >= 21
    pid_t pthread_gettid_np(pthread_t __pthread) __INTRODUCED_IN(21);
    #endif /* __ANDROID_API__ >= 21 */
    
    
    /**
     * 线程等待
     * 调用该函数的线程将挂起等待,直到id为__pthread的线程终止。
     * @param __pthread
     * @param __return_value_ptr
     * @return
     */
    int pthread_join(pthread_t __pthread, void** __return_value_ptr);
    
    int pthread_key_create(pthread_key_t* __key_ptr, void (*__key_destructor)(void*));
    int pthread_key_delete(pthread_key_t __key);
    
    int pthread_mutexattr_destroy(pthread_mutexattr_t* __attr);
    int pthread_mutexattr_getpshared(const pthread_mutexattr_t* __attr, int* __shared);
    int pthread_mutexattr_gettype(const pthread_mutexattr_t* __attr, int* __type);
    
    int pthread_mutexattr_init(pthread_mutexattr_t* __attr);
    int pthread_mutexattr_setpshared(pthread_mutexattr_t* __attr, int __shared);
    int pthread_mutexattr_settype(pthread_mutexattr_t* __attr, int __type);
    
    //销毁互斥量
    int pthread_mutex_destroy(pthread_mutex_t* __mutex);
    // 创建互斥量
    int pthread_mutex_init(pthread_mutex_t* __mutex, const pthread_mutexattr_t* __attr);
    //锁住互斥量
    int pthread_mutex_lock(pthread_mutex_t* __mutex);
    
    #if __ANDROID_API__ >= 21
    int pthread_mutex_timedlock(pthread_mutex_t* __mutex, const struct timespec* __timeout)
      __INTRODUCED_IN(21);
    #endif /* __ANDROID_API__ >= 21 */
    
    //非阻塞锁住互斥量
    int pthread_mutex_trylock(pthread_mutex_t* __mutex);
    // 解锁互斥量
    int pthread_mutex_unlock(pthread_mutex_t* __mutex);
    
    #if __ANDROID_API__ < 21
    /*
     * Cruft for supporting old API levels. Pre-L we didn't have the proper POSIX
     * APIs for things, but instead had some locally grown, artisan equivalents.
     * Keep exposing the old prototypes on old API levels so we don't regress
     * functionality.
     *
     * See the following bugs:
     *  * https://github.com/android-ndk/ndk/issues/420
     *  * https://github.com/android-ndk/ndk/issues/423
     *  * https://stackoverflow.com/q/44580542/632035
     */
    int pthread_mutex_lock_timeout_np(pthread_mutex_t* __mutex, unsigned __timeout_ms);
    int pthread_cond_timeout_np(pthread_cond_t* __cond, pthread_mutex_t* __mutex, unsigned __timeout_ms);
    int pthread_cond_timedwait_monotonic_np(pthread_cond_t* __cond, pthread_mutex_t* __mutex, const struct timespec* __timeout);
    int pthread_cond_timedwait_relative_np(pthread_cond_t* __cond, pthread_mutex_t* __mutex, const struct timespec* __relative_timeout);
    #endif
    
    int pthread_once(pthread_once_t* __once, void (*__init_routine)(void));
    
    int pthread_rwlockattr_init(pthread_rwlockattr_t* __attr);
    int pthread_rwlockattr_destroy(pthread_rwlockattr_t* __attr);
    int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t* __attr, int* __shared);
    int pthread_rwlockattr_setpshared(pthread_rwlockattr_t* __attr, int __shared);
    
    #if __ANDROID_API__ >= 23
    int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t* __attr, int* __kind)
      __INTRODUCED_IN(23);
    int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t* __attr, int __kind) __INTRODUCED_IN(23);
    #endif /* __ANDROID_API__ >= 23 */
    
    //读写锁 同一时间,可以有多个线程获取到 读锁, 但只有一个线程可以获取到 写锁.
    //销毁
    int pthread_rwlock_destroy(pthread_rwlock_t* __rwlock);
    // 创建
    int pthread_rwlock_init(pthread_rwlock_t* __rwlock, const pthread_rwlockattr_t* __attr);
    // 获取读锁
    int pthread_rwlock_rdlock(pthread_rwlock_t* __rwlock);
    // 获取 读锁  设置了读锁的时间
    int pthread_rwlock_timedrdlock(pthread_rwlock_t* __rwlock, const struct timespec* __timeout);
    // 获取 写锁  设置了写锁的时间
    int pthread_rwlock_timedwrlock(pthread_rwlock_t* __rwlock, const struct timespec* __timeout);
    // 非阻塞获取读锁
    int pthread_rwlock_tryrdlock(pthread_rwlock_t* __rwlock);
    //非阻塞获取写锁
    int pthread_rwlock_trywrlock(pthread_rwlock_t* __rwlock);
    //释放锁
    int pthread_rwlock_unlock(pthread_rwlock_t* __rwlock);
    //获取写锁
    int pthread_rwlock_wrlock(pthread_rwlock_t* __rwlock);
    
    #if __ANDROID_API__ >= __ANDROID_API_N__
    int pthread_barrierattr_init(pthread_barrierattr_t* __attr) __INTRODUCED_IN(24);
    int pthread_barrierattr_destroy(pthread_barrierattr_t* __attr) __INTRODUCED_IN(24);
    int pthread_barrierattr_getpshared(const pthread_barrierattr_t* __attr, int* __shared) __INTRODUCED_IN(24);
    int pthread_barrierattr_setpshared(pthread_barrierattr_t* __attr, int __shared) __INTRODUCED_IN(24);
    #endif
    
    #if __ANDROID_API__ >= __ANDROID_API_N__
    int pthread_barrier_init(pthread_barrier_t* __barrier, const pthread_barrierattr_t* __attr, unsigned __count) __INTRODUCED_IN(24);
    int pthread_barrier_destroy(pthread_barrier_t* __barrier) __INTRODUCED_IN(24);
    int pthread_barrier_wait(pthread_barrier_t* __barrier) __INTRODUCED_IN(24);
    #endif
    
    #if __ANDROID_API__ >= __ANDROID_API_N__
    int pthread_spin_destroy(pthread_spinlock_t* __spinlock) __INTRODUCED_IN(24);
    int pthread_spin_init(pthread_spinlock_t* __spinlock, int __shared) __INTRODUCED_IN(24);
    int pthread_spin_lock(pthread_spinlock_t* __spinlock) __INTRODUCED_IN(24);
    int pthread_spin_trylock(pthread_spinlock_t* __spinlock) __INTRODUCED_IN(24);
    int pthread_spin_unlock(pthread_spinlock_t* __spinlock) __INTRODUCED_IN(24);
    #endif
    
    //函数用于获得本线程的pthread_t
    pthread_t pthread_self(void) __attribute_const__;
    
    #if defined(__USE_GNU)
    
    #if __ANDROID_API__ >= 26
    int pthread_getname_np(pthread_t __pthread, char* __buf, size_t __n) __INTRODUCED_IN(26);
    #endif /* __ANDROID_API__ >= 26 */
    
    #endif
    /* TODO: this should be __USE_GNU too. */
    int pthread_setname_np(pthread_t __pthread, const char* __name);
    
    int pthread_setschedparam(pthread_t __pthread, int __policy, const struct sched_param* __param);
    
    #if __ANDROID_API__ >= __ANDROID_API_FUTURE__
    int pthread_setschedprio(pthread_t __pthread, int __priority) __INTRODUCED_IN_FUTURE;
    #endif /* __ANDROID_API__ >= __ANDROID_API_FUTURE__ */
    
    
    int pthread_setspecific(pthread_key_t __key, const void* __value);
    
    typedef void (*__pthread_cleanup_func_t)(void*);
    
    typedef struct __pthread_cleanup_t {
        struct __pthread_cleanup_t*   __cleanup_prev;
        __pthread_cleanup_func_t      __cleanup_routine;
        void*                         __cleanup_arg;
    } __pthread_cleanup_t;
    
    // 注册线程结束时 自动调用的清理函数 注册的函数安装顺序压入一个栈内,调用的时候就一个个的出栈。
    void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*);
    // 调用栈顶函数
    void __pthread_cleanup_pop(__pthread_cleanup_t*, int);
    
    /* Believe or not, the definitions of pthread_cleanup_push and
     * pthread_cleanup_pop below are correct. Posix states that these
     * can be implemented as macros that might introduce opening and
     * closing braces, and that using setjmp/longjmp/return/break/continue
     * between them results in undefined behavior.
     */
    #define  pthread_cleanup_push(routine, arg)                      \
        do {                                                         \
            __pthread_cleanup_t  __cleanup;                          \
            __pthread_cleanup_push( &__cleanup, (routine), (arg) );  \
    
    #define  pthread_cleanup_pop(execute)                  \
            __pthread_cleanup_pop( &__cleanup, (execute)); \
        } while (0);                                       \
    
    __END_DECLS
    
    #endif
    
    
    

    相关文章

      网友评论

          本文标题:pthread 函数解释

          本文链接:https://www.haomeiwen.com/subject/eyillctx.html