Blogger Information
Blog 35
fans 0
comment 0
visits 29169
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
swoole锁的机制代码实例讲解
P粉526161432
Original
507 people have browsed it
  1. 这篇文章主要介绍了swoole锁的机制代码实例讲解,对于锁的介绍讲解的很清晰,有感兴趣的同学可以学习下
  1. 锁,这个词我们并不陌生,主要的应用场景会发生在高并发下进行锁。今天的这篇文章咱们主要来讲解一下swoole的锁的机制,swoole_lock是如何实现的。
  2. swoole_lock类支持5种锁的类型:
  3. 文件锁 SWOOLE_FILELOCK
  4. 读写锁 SWOOLE_RWLOCK
  5. 信号量 SWOOLE_SEM
  6. 互斥锁 SWOOLE_MUTEX
  7. 自旋锁 SWOOLE_SPINLOCK
  8. 创建这些锁的过程其实就是调用构造函数的过程,调用的形式如下:
  9. swoole_lock->__construct(int $type, [string $lockfile])
  10. $type为锁的类型
  11. $lockfile,当类型为SWOOLE_FILELOCK时必须传入,指定文件锁的路径
  12. 下面我们介绍下这个锁的实现
  1. static PHP_METHOD(swoole_lock, __construct)
  2. {
  3. long type = SW_MUTEX;
  4. char *filelock;
  5. zend_size_t filelock_len = 0;
  6. int ret;
  7. //解析输入参数,这里输入参数有2个,其中type表示锁的类型,另外个参数是文件锁时必须传入(表示文件锁对应的文件路径),其他锁时,不需要这个参数
  8. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ls", &type, &filelock, &filelock_len) == FAILURE)
  9. {
  10. RETURN_FALSE;
  11. }
  12. //从内存池申请锁对象空间,这里仅仅是申请锁空间
  13. swLock *lock = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swLock));
  14. if (lock == NULL)//申请空间失败
  15. {
  16. zend_throw_exception(swoole_exception_class_entry_ptr, "global memory allocation failure.", SW_ERROR_MALLOC_FAIL TSRMLS_CC);
  17. RETURN_FALSE;
  18. }
  19. switch(type)//按type遍历,创建锁对象
  20. {
  21. #ifdef HAVE_RWLOCK
  22. case SW_RWLOCK://如果是读写锁
  23. ret = swRWLock_create(lock, 1);//创建锁对象,类型为读写锁
  24. break;
  25. #endif
  26. case SW_FILELOCK://如果是文件锁
  27. if (filelock_len <= 0)//第二个参数有效性检查
  28. {
  29. zend_throw_exception(swoole_exception_class_entry_ptr, "filelock requires file name of the lock.", SW_ERROR_INVALID_PARAMS TSRMLS_CC);
  30. RETURN_FALSE;
  31. }
  32. int fd;
  33. if ((fd = open(filelock, O_RDWR | O_CREAT, 0666)) < 0) //调用linux函数open,打开文件(不存在则创建)
  34. {
  35. zend_throw_exception_ex(swoole_exception_class_entry_ptr, errno TSRMLS_CC, "open file[%s] failed. Error: %s [%d]", filelock, strerror(errno), errno);
  36. RETURN_FALSE;
  37. }
  38. ret = swFileLock_create(lock, fd);//创建锁对象,类型为文件锁
  39. break;
  40. case SW_SEM:
  41. ret = swSem_create(lock, IPC_PRIVATE);//创建锁对象,类型为信号量
  42. break;
  43. #ifdef HAVE_SPINLOCK
  44. case SW_SPINLOCK:
  45. ret = swSpinLock_create(lock, 1);//创建锁对象,类型为乐观锁
  46. break;
  47. #endif
  48. case SW_MUTEX:
  49. default:
  50. ret = swMutex_create(lock, 1);//创建锁对象,类型为互斥量
  51. break;
  52. }
  53. if (ret < 0)
  54. {
  55. zend_throw_exception(swoole_exception_class_entry_ptr, "failed to create lock.", errno TSRMLS_CC);
  56. RETURN_FALSE;
  57. }
  58. swoole_set_object(getThis(), lock);//PHP侧的对象和swoole内部对象关联
  59. RETURN_TRUE;
  60. }
  1. 以下分别介绍下各个不同锁对象的创建过程。
  2. 1、读写锁
  1. int swRWLock_create(swLock *lock, int use_in_process)
  2. {
  3. int ret;
  4. bzero(lock, sizeof(swLock));//锁空间初始化
  5. lock->type = SW_RWLOCK;//设置锁的类型为读写锁
  6. pthread_rwlockattr_init(&lock->object.rwlock.attr);//linux函数,锁属性信息初始化
  7. if (use_in_process == 1)//标记为在进程中使用,这里pthread开头的linux函数默认都是针对线程的
  8. {
  9. //设置锁的属性信息,标记为在进程中使用
  10. pthread_rwlockattr_setpshared(&lock->object.rwlock.attr, PTHREAD_PROCESS_SHARED);
  11. }
  12. if ((ret = pthread_rwlock_init(&lock->object.rwlock._lock, &lock->object.rwlock.attr)) < 0)//linux函数,锁信息初始化
  13. {
  14. return SW_ERR;
  15. }
  16. /*
  17. * 设置锁的回调函数
  18. */
  19. lock->lock_rd = swRWLock_lock_rd;
  20. lock->lock = swRWLock_lock_rw;
  21. lock->unlock = swRWLock_unlock;
  22. lock->trylock = swRWLock_trylock_rw;
  23. lock->trylock_rd = swRWLock_trylock_rd;
  24. lock->free = swRWLock_free;
  25. return SW_OK;
  26. }
  1. 2、文件锁。
  1. int swFileLock_create(swLock *lock, int fd)
  2. {
  3. bzero(lock, sizeof(swLock));//锁对象信息初始化
  4. lock->type = SW_FILELOCK;//设置锁的类型为文件锁
  5. /*
  6. * 设置锁的回调函数
  7. */
  8. lock->object.filelock.fd = fd;
  9. lock->lock_rd = swFileLock_lock_rd;
  10. lock->lock = swFileLock_lock_rw;
  11. lock->trylock_rd = swFileLock_trylock_rd;
  12. lock->trylock = swFileLock_trylock_rw;
  13. lock->unlock = swFileLock_unlock;
  14. lock->free = swFileLock_free;
  15. return 0;
  16. }
  1. 3、信号量锁
  1. int swSem_create(swLock *lock, key_t key)
  2. {
  3. int ret;
  4. lock->type = SW_SEM;//设置锁类型为信号量锁
  5. if ((ret = semget(key, 1, IPC_CREAT | 0666)) < 0)//创建信号量,这里设置的属性IPC_CREAT,这表示这种信号量只能用于有亲缘关系的进程间
  6. {
  7. return SW_ERR;
  8. }
  9. if (semctl(ret, 0, SETVAL, 1) == -1)//设置信号量ret的值为1
  10. {
  11. swWarn("semctl(SETVAL) failed");
  12. return SW_ERR;
  13. }
  14. lock->object.sem.semid = ret;//设置信号量ID
  15. /*
  16. * 设置回调函数
  17. */
  18. lock->lock = swSem_lock;
  19. lock->unlock = swSem_unlock;
  20. lock->free = swSem_free;
  21. return SW_OK;
  22. }
  1. 4、乐观锁
  1. int swSpinLock_create(swLock *lock, int use_in_process)
  2. {
  3. int ret;
  4. bzero(lock, sizeof(swLock));//初始化锁对象
  5. lock->type = SW_SPINLOCK;//设置锁的类型为乐观锁
  6. //执行锁的初始化操作,这里指明是在多进程中使用
  7. if ((ret = pthread_spin_init(&lock->object.spinlock.lock_t, use_in_process)) < 0)
  8. {
  9. return -1;
  10. }
  11. /*
  12. * 设置回调函数信息
  13. */
  14. lock->lock = swSpinLock_lock;
  15. lock->unlock = swSpinLock_unlock;
  16. lock->trylock = swSpinLock_trylock;
  17. lock->free = swSpinLock_free;
  18. return 0;
  19. }
  1. 5、互斥量锁
  1. int swMutex_create(swLock *lock, int use_in_process)
  2. {
  3. int ret;
  4. bzero(lock, sizeof(swLock));
  5. lock->type = SW_MUTEX;
  6. pthread_mutexattr_init(&lock->object.mutex.attr);
  7. if (use_in_process == 1)
  8. {
  9. pthread_mutexattr_setpshared(&lock->object.mutex.attr, PTHREAD_PROCESS_SHARED);
  10. }
  11. if ((ret = pthread_mutex_init(&lock->object.mutex._lock, &lock->object.mutex.attr)) < 0)
  12. {
  13. return SW_ERR;
  14. }
  15. lock->lock = swMutex_lock;
  16. lock->unlock = swMutex_unlock;
  17. lock->trylock = swMutex_trylock;
  18. lock->free = swMutex_free;
  19. return SW_OK;
  20. }
  1. 到此这篇关于swoole锁的机制代码实例讲解的文章就介绍到这了,更多相关swoole锁的机制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post