123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #include <winpr/synch.h>
- #include "precompile.h"
- #include "sp_uid.h"
- #include "spinlock.h"
- #include "memutil.h"
- #include "y2k_time.h"
- #include "dbgutil.h"
- struct sp_uid_generator_t
- {
- unsigned short app_id;
- lock_t lock;
- struct _sp_uid_t last_id;
- int mt;
- };
- sp_uid_generator_t *sp_uid_generator_create(unsigned short app_id, int mt)
- {
- sp_uid_generator_t *uid_gen = MALLOC_T(sp_uid_generator_t);
- if (uid_gen) {
- uid_gen->app_id = app_id;
- uid_gen->mt = mt;
- if (mt) {
- fastlock_init(uid_gen->lock);
- }
- uid_gen->last_id.app_id = app_id;
- uid_gen->last_id.dsn = 0;
- uid_gen->last_id.tick = y2k_time_now();
- }
- return uid_gen;
- }
- void sp_uid_generator_destroy(sp_uid_generator_t *uid)
- {
- if (uid->mt) {
- fastlock_term(uid->lock);
- }
- free(uid);
- }
- sp_uid_t sp_uid_generator_new_id(sp_uid_generator_t *uid)
- {
- struct _sp_uid_t r;
- TOOLKIT_ASSERT(uid);
- r.app_id = uid->app_id;
- repeat:
- r.tick = y2k_time_now();
- if (uid->mt) {
- fastlock_enter(uid->lock);
- }
- if (r.tick == uid->last_id.tick) {
- r.dsn = uid->last_id.dsn + 1;
- if (r.dsn == 0) {
- if (uid->mt) {
- fastlock_leave(uid->lock);
- }
- Sleep(1);
- goto repeat;
- }
- } else {
- r.dsn = 0;
- }
- uid->last_id.dsn = r.dsn;
- uid->last_id.tick = r.tick;
- if (uid->mt) {
- fastlock_leave(uid->lock);
- }
- return r.id;
- }
- sp_uid_t sp_uid_make(unsigned short app_id)
- {
- struct _sp_uid_t r;
- r.app_id = app_id;
- r.dsn = 0;
- r.tick = y2k_time_now();
- return r.id;
- }
- sp_uid_t sp_uid_update(sp_uid_t t)
- {
- y2k_time_t now;
- struct _sp_uid_t r;
- r.id = t;
- repeat:
- now = y2k_time_now();
- if (now == r.tick) {
- r.dsn++;
- if (r.dsn == 0) {
- Sleep(1);
- goto repeat;
- }
- } else {
- r.dsn = 0;
- r.tick = now;
- }
- return r.id;
- }
|