summaryrefslogtreecommitdiff
path: root/src/cl_base_object.c
blob: 5578bdcc9e464617e0baf22b3f30984cad72019e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include <stdio.h>
#include "cl_base_object.h"

static pthread_t invalid_thread_id = -1;

LOCAL void
cl_object_init_base(cl_base_object obj, cl_ulong magic)
{
  obj->magic = magic;
  obj->ref = 1;
  SET_ICD(obj->dispatch);
  pthread_mutex_init(&obj->mutex, NULL);
  pthread_cond_init(&obj->cond, NULL);
  obj->owner = invalid_thread_id;
  list_node_init(&obj->node);
}

LOCAL void
cl_object_destroy_base(cl_base_object obj)
{
  int ref = CL_OBJECT_GET_REF(obj);
  if (ref != 0) {
    DEBUGP(DL_ERROR, "CL object %p, call destroy with a reference %d", obj,
           ref);
    assert(0);
  }

  if (!CL_OBJECT_IS_VALID(obj)) {
    DEBUGP(DL_ERROR,
           "CL object %p, call destroy while it is already a dead object", obj);
    assert(0);
  }

  if (obj->owner != invalid_thread_id) {
    DEBUGP(DL_ERROR, "CL object %p, call destroy while still has a owener %d",
           obj, (int)obj->owner);
    assert(0);
  }

  if (!list_node_out_of_list(&obj->node)) {
    DEBUGP(DL_ERROR, "CL object %p, call destroy while still belong to some object %p",
           obj, obj->node.p);
    assert(0);
  }

  obj->magic = CL_OBJECT_INVALID_MAGIC;
  pthread_mutex_destroy(&obj->mutex);
  pthread_cond_destroy(&obj->cond);
}

LOCAL cl_int
cl_object_take_ownership(cl_base_object obj, cl_int wait, cl_bool withlock)
{
  pthread_t self;

  assert(CL_OBJECT_IS_VALID(obj));

  self = pthread_self();

  if (withlock == CL_FALSE)
    pthread_mutex_lock(&obj->mutex);

  if (pthread_equal(obj->owner, self)) { // Already get
    if (withlock == CL_FALSE)
      pthread_mutex_unlock(&obj->mutex);
    return 1;
  }

  if (pthread_equal(obj->owner, invalid_thread_id)) {
    obj->owner = self;

    if (withlock == CL_FALSE)
      pthread_mutex_unlock(&obj->mutex);
    return 1;
  }

  if (wait == 0) {
    if (withlock == CL_FALSE)
      pthread_mutex_unlock(&obj->mutex);
    return 0;
  }

  while (!pthread_equal(obj->owner, invalid_thread_id)) {
    pthread_cond_wait(&obj->cond, &obj->mutex);
  }

  obj->owner = self;

  if (withlock == CL_FALSE)
    pthread_mutex_unlock(&obj->mutex);

  return 1;
}

LOCAL void
cl_object_release_ownership(cl_base_object obj, cl_bool withlock)
{
  assert(CL_OBJECT_IS_VALID(obj));

  if (withlock == CL_FALSE)
    pthread_mutex_lock(&obj->mutex);

  assert(pthread_equal(pthread_self(), obj->owner));
  obj->owner = invalid_thread_id;
  pthread_cond_broadcast(&obj->cond);

  if (withlock == CL_FALSE)
    pthread_mutex_unlock(&obj->mutex);
}

LOCAL void
cl_object_wait_on_cond(cl_base_object obj)
{
  assert(CL_OBJECT_IS_VALID(obj));
  pthread_cond_wait(&obj->cond, &obj->mutex);
}

LOCAL void
cl_object_notify_cond(cl_base_object obj)
{
  assert(CL_OBJECT_IS_VALID(obj));
  pthread_cond_broadcast(&obj->cond);
}