Atlas Runtime
region_test.h
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2016 Hewlett Packard Enterprise Development LP
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version. This program is
8  * distributed in the hope that it will be useful, but WITHOUT ANY
9  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11  * for more details. You should have received a copy of the GNU Lesser
12  * General Public License along with this program. If not, see
13  * <http://www.gnu.org/licenses/>.
14  */
15 
16 #include "atlas_alloc.h"
17 #include "atlas_api.h"
18 
19 #include <assert.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 
25 #define THREADS 4
26 #define WORK 100
27 #define NUM_NODES 300
28 
29 typedef struct node node;
30 
31 struct node {
33  int work;
34 };
35 
37 pthread_mutex_t root_lock;
39 uint32_t global_rgn_id;
40 
41 void *add() {
42  node *current;
43  int i;
44 
45  pthread_mutex_lock(&root_lock);
46  if (num_nodes >= NUM_NODES) {
47  pthread_mutex_unlock(&root_lock);
48  return 0;
49  }
50  i = num_nodes;
51  pthread_mutex_unlock(&root_lock);
52 
53  while (i < NUM_NODES) {
54  current = (node *)nvm_alloc(sizeof(node), global_rgn_id);
55  current->work = (i * WORK) ^ i;
56 
57  pthread_mutex_lock(&root_lock);
58  if (num_nodes >= NUM_NODES) {
59  nvm_free(current);
60  pthread_mutex_unlock(&root_lock);
61  break;
62  }
63  i = ++num_nodes;
64  printf("Adding node number %i\n", i);
65  root->next = current;
66  root = current;
67  pthread_mutex_unlock(&root_lock);
68  }
69  return 0;
70 }
71 
72 void dump() {
73  while (root != NULL) {
74  printf("Data is %i\n", root->work);
75  root = root->next;
76  }
77 }
78 
79 void test(uint32_t rgn_id) {
80  int i;
81  pthread_attr_t attr;
82  pthread_t *tid;
84 
85  root = (node *)NVM_GetRegionRoot(rgn_id);
86 
87  if (!root) {
88  root = (node *)nvm_alloc(sizeof(node), rgn_id);
89  root->next = NULL;
90  root->work = WORK;
91  num_nodes = 1;
92  NVM_SetRegionRoot(rgn_id, root);
93  }
94 
95  if (root->next == NULL) {
96  assert(root && "Region root is NULL");
97 
98  pthread_mutex_init(&root_lock, NULL);
99  tid = (pthread_t *)malloc(THREADS * sizeof(pthread_t));
100  pthread_attr_init(&attr);
101  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
102 
103  for (i = 0; i < THREADS; i++) {
104  printf("Creating thead %i\n", i);
105  pthread_create(&tid[i], &attr, (void *(*)(void *))add, NULL);
106  // pthread_create(&tid[i], &attr, *operation, NULL);
107  }
108 
109  printf("Waiting for threads");
110 
111  for (i = 0; i < THREADS; i++) {
112  pthread_join(tid[i], NULL);
113  }
114 
115  free(tid);
116  } else {
117  dump();
118  }
119 
120  sleep(2);
121 }
Definition: region_test.h:31
node * next
Definition: region_test.h:32
#define WORK
Definition: region_test.h:26
#define THREADS
Definition: region_test.h:25
void dump()
Definition: region_test.h:72
#define NUM_NODES
Definition: region_test.h:27
node * root
Definition: region_test.h:36
void NVM_SetRegionRoot(uint32_t rid, void *root)
Set the root pointer of an existing persistent region.
Definition: pregion_mgr_api.cpp:55
uint32_t global_rgn_id
Definition: region_test.h:39
int work
Definition: region_test.h:33
void * nvm_alloc(size_t sz, uint32_t rid)
Malloc style interface for allocation from a persistent region.
Definition: pregion_mgr_api.cpp:60
void test(uint32_t rgn_id)
Definition: region_test.h:79
void nvm_free(void *ptr)
Deallocation interface for persistent data.
Definition: pregion_mgr_api.cpp:78
uint32_t rgn_id
Definition: malloc_free_test.c:22
void * NVM_GetRegionRoot(uint32_t rid)
Get the root pointer of the persistent region.
Definition: pregion_mgr_api.cpp:50
pthread_mutex_t root_lock
Definition: region_test.h:37
int num_nodes
Definition: region_test.h:38
void * add()
Definition: region_test.h:41