2 * Copyright (C) 2017 Cisco Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // @author Changxue Deng <chadeng@cisco.com>
19 #ifndef __ASYNC_WRITER_H__
20 #define __ASYNC_WRITER_H__
27 #include "mb_backup.h"
31 #define MABAIN_ASYNC_TYPE_NONE 0
32 #define MABAIN_ASYNC_TYPE_ADD 1
33 #define MABAIN_ASYNC_TYPE_REMOVE 2
34 #define MABAIN_ASYNC_TYPE_REMOVE_ALL 3
35 #define MABAIN_ASYNC_TYPE_RC 4
36 #define MABAIN_ASYNC_TYPE_BACKUP 5
38 #define MB_ASYNC_SHM_KEY_SIZE 256
39 #define MB_ASYNC_SHM_DATA_SIZE 1024
40 #define MB_ASYNC_SHM_LOCK_TMOUT 5
43 typedef struct _AsyncNode
45 std::atomic<bool> in_use;
46 pthread_mutex_t mutex;
50 char key[MB_ASYNC_SHM_KEY_SIZE];
51 char data[MB_ASYNC_SHM_DATA_SIZE];
66 AsyncWriter(DB *db_ptr);
70 void UpdateNumUsers(int delta);
71 int Add(const char *key, int key_len, const char *data, int data_len, bool overwrite);
72 int Remove(const char *key, int len);
74 int Backup(const char *backup_dir);
75 int CollectResource(int64_t m_index_rc_size, int64_t m_data_rc_size,
76 int64_t max_dbsz, int64_t max_dbcnt);
80 int StopAsyncThread();
81 int ProcessTask(int ntasks, bool rc_mode);
84 static void *async_thread_wrapper(void *context);
85 AsyncNode* AcquireSlot();
86 int PrepareSlot(AsyncNode *node_ptr) const;
87 void* async_writer_thread();
89 uint32_t NextShmSlot(uint32_t windex, uint32_t qindex);
104 std::atomic<int> num_users;
105 std::atomic<uint32_t> queue_index;
106 uint32_t writer_index;