tidpool.c 3.7 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
log  
Shengliang Guan 已提交
16
#include "tlog.h"
H
hzcheng 已提交
17 18 19 20 21

typedef struct {
  int             maxId;
  int             numOfFree;
  int             freeSlot;
S
slguan 已提交
22
  bool *          freeList;
H
hzcheng 已提交
23 24 25 26
  pthread_mutex_t mutex;
} id_pool_t;

void *taosInitIdPool(int maxId) {
S
slguan 已提交
27
  id_pool_t *pIdPool = calloc(1, sizeof(id_pool_t));
H
hzcheng 已提交
28 29
  if (pIdPool == NULL) return NULL;

S
slguan 已提交
30
  pIdPool->freeList = calloc(maxId, sizeof(bool));
S
slguan 已提交
31
  if (pIdPool->freeList == NULL) {
H
hzcheng 已提交
32 33 34 35 36
    free(pIdPool);
    return NULL;
  }

  pIdPool->maxId = maxId;
S
slguan 已提交
37
  pIdPool->numOfFree = maxId;
H
hzcheng 已提交
38 39 40 41
  pIdPool->freeSlot = 0;

  pthread_mutex_init(&pIdPool->mutex, NULL);

42
  uDebug("pool:%p is setup, maxId:%d", pIdPool, pIdPool->maxId);
H
hzcheng 已提交
43

S
slguan 已提交
44
  return pIdPool;
H
hzcheng 已提交
45 46 47
}

int taosAllocateId(void *handle) {
S
slguan 已提交
48 49 50 51
  id_pool_t *pIdPool = handle;
  if (handle == NULL) {
    return -1;
  }
H
hzcheng 已提交
52

S
slguan 已提交
53 54
  int slot = -1;
  pthread_mutex_lock(&pIdPool->mutex);
H
hzcheng 已提交
55 56

  if (pIdPool->numOfFree > 0) {
S
slguan 已提交
57 58
    for (int i = 0; i < pIdPool->maxId; ++i) {
      slot = (i + pIdPool->freeSlot) % pIdPool->maxId;
S
slguan 已提交
59 60
      if (!pIdPool->freeList[slot]) {
        pIdPool->freeList[slot] = true;
S
slguan 已提交
61 62 63 64 65
        pIdPool->freeSlot = slot + 1;
        pIdPool->numOfFree--;
        break;
      }
    }
H
hzcheng 已提交
66 67
  }

S
slguan 已提交
68
  pthread_mutex_unlock(&pIdPool->mutex);
S
slguan 已提交
69
  return slot + 1;
H
hzcheng 已提交
70 71 72
}

void taosFreeId(void *handle, int id) {
S
slguan 已提交
73 74
  id_pool_t *pIdPool = handle;
  if (handle == NULL) return;
H
hzcheng 已提交
75

S
slguan 已提交
76
  pthread_mutex_lock(&pIdPool->mutex);
H
hzcheng 已提交
77

S
slguan 已提交
78 79 80
  int slot = (id - 1) % pIdPool->maxId;
  if (pIdPool->freeList[slot]) {
    pIdPool->freeList[slot] = false;
S
slguan 已提交
81 82
    pIdPool->numOfFree++;
  }
H
hzcheng 已提交
83

S
slguan 已提交
84
  pthread_mutex_unlock(&pIdPool->mutex);
H
hzcheng 已提交
85 86 87
}

void taosIdPoolCleanUp(void *handle) {
S
slguan 已提交
88
  id_pool_t *pIdPool = handle;
H
hzcheng 已提交
89

S
slguan 已提交
90
  if (pIdPool == NULL) return;
H
hzcheng 已提交
91

92
  uDebug("pool:%p is cleaned", pIdPool);
H
hzcheng 已提交
93 94 95 96 97 98 99 100 101 102 103

  if (pIdPool->freeList) free(pIdPool->freeList);

  pthread_mutex_destroy(&pIdPool->mutex);

  memset(pIdPool, 0, sizeof(id_pool_t));

  free(pIdPool);
}

int taosIdPoolNumOfUsed(void *handle) {
S
slguan 已提交
104
  id_pool_t *pIdPool = handle;
105 106 107 108 109 110

  pthread_mutex_lock(&pIdPool->mutex);
  int ret = pIdPool->maxId - pIdPool->numOfFree;
  pthread_mutex_unlock(&pIdPool->mutex);

  return ret;
H
hzcheng 已提交
111 112
}

113 114
bool taosIdPoolMarkStatus(void *handle, int id) {
  bool ret = false;
S
slguan 已提交
115 116
  id_pool_t *pIdPool = handle;
  pthread_mutex_lock(&pIdPool->mutex);
H
hzcheng 已提交
117

S
slguan 已提交
118 119 120
  int slot = (id - 1) % pIdPool->maxId;
  if (!pIdPool->freeList[slot]) {
    pIdPool->freeList[slot] = true;
S
slguan 已提交
121
    pIdPool->numOfFree--;
122 123 124
    ret = true;
  } else {
    ret = false;
H
hzcheng 已提交
125
  }
S
slguan 已提交
126 127

  pthread_mutex_unlock(&pIdPool->mutex);
128
  return ret;
H
hzcheng 已提交
129
}
S
slguan 已提交
130 131 132 133

int taosUpdateIdPool(id_pool_t *handle, int maxId) {
  id_pool_t *pIdPool = (id_pool_t*)handle;
  if (maxId <= pIdPool->maxId) {
134
    return 0;
S
slguan 已提交
135 136
  }

S
slguan 已提交
137
  bool *idList = calloc(maxId, sizeof(bool));
S
slguan 已提交
138 139 140 141
  if (idList == NULL) {
    return -1;
  }

S
slguan 已提交
142 143
  pthread_mutex_lock(&pIdPool->mutex);

S
slguan 已提交
144
  memcpy(idList, pIdPool->freeList, sizeof(bool) * pIdPool->maxId);
S
slguan 已提交
145 146 147
  pIdPool->numOfFree += (maxId - pIdPool->maxId);
  pIdPool->maxId = maxId;

S
slguan 已提交
148
  bool *oldIdList = pIdPool->freeList;
S
slguan 已提交
149 150 151
  pIdPool->freeList = idList;
  free(oldIdList);

S
slguan 已提交
152
  pthread_mutex_unlock(&pIdPool->mutex);
S
slguan 已提交
153 154 155 156 157

  return 0;
}

int taosIdPoolMaxSize(void *handle) {
158 159 160 161 162 163 164
  id_pool_t *pIdPool = (id_pool_t *)handle;

  pthread_mutex_lock(&pIdPool->mutex);
  int ret = pIdPool->maxId;
  pthread_mutex_unlock(&pIdPool->mutex);

  return ret;
S
slguan 已提交
165
}