OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SchemeSmobAS.cc
Go to the documentation of this file.
1 /*
2  * SchemeSmobAS.c
3  *
4  * Scheme small objects (SMOBS) for atom spaces.
5  *
6  * Copyright (c) 2008,2009,2014 Linas Vepstas <linas@linas.org>
7  */
8 
9 #ifdef HAVE_GUILE
10 
11 #include <cstddef>
12 #include <libguile.h>
13 
16 
17 using namespace opencog;
18 
19 // Need a lock to protect the map, since multiple threads may be trying
20 // to update this map. The map contains a use-count for the number of
21 // threads that are currently using this atomspace as the current
22 // attomspace. When the count drops to zero, the atomspace will be
23 // reaped if the number of SCM references also drops to zero (and the
24 // GC runs).
25 std::mutex SchemeSmob::as_mtx;
26 std::map<AtomSpace*, int> SchemeSmob::deleteable_as;
27 
28 /* ============================================================== */
29 
30 std::string SchemeSmob::as_to_string(const AtomSpace *as)
31 {
32 #define BUFLEN 120
33  char buff[BUFLEN];
34 
35  snprintf(buff, BUFLEN, "#<atomspace %p>", as);
36  return buff;
37 }
38 
39 /* ============================================================== */
45 {
46  SCM smob;
47  SCM_NEWSMOB (smob, cog_misc_tag, as);
48  SCM_SET_SMOB_FLAGS(smob, COG_AS);
49  return smob;
50 }
51 
52 /* ============================================================== */
57 {
58  scm_gc_register_collectable_memory (as,
59  sizeof(*as), "opencog atomspace");
60 
61  return make_as(as);
62 }
63 
64 /* ============================================================== */
70 {
71  AtomSpace *parent = ss_to_atomspace(s);
72 
73  AtomSpace *as = new AtomSpace(parent);
74 
75  // Only the internally-created atomspaces are trackable.
76  deleteable_as[as] = 0;
77  return take_as(as);
78 }
79 
80 /* ============================================================== */
85 {
86  if (SCM_SMOB_PREDICATE(SchemeSmob::cog_misc_tag, s))
87  {
88  scm_t_bits misctype = SCM_SMOB_FLAGS(s);
89  switch (misctype)
90  {
91  case COG_AS:
92  return SCM_BOOL_T;
93 
94  default:
95  return SCM_BOOL_F;
96  }
97  }
98  return SCM_BOOL_F;
99 }
100 
101 /* ============================================================== */
102 /* Cast SCM to atomspace */
103 
105 {
106  if (not SCM_SMOB_PREDICATE(SchemeSmob::cog_misc_tag, sas))
107  return NULL;
108 
109  scm_t_bits misctype = SCM_SMOB_FLAGS(sas);
110  if (COG_AS != misctype)
111  return NULL;
112 
113  return (AtomSpace *) SCM_SMOB_DATA(sas);
114 }
115 
116 /* ============================================================== */
121 
123 {
124  return scm_fluid_ref(atomspace_fluid);
125 }
126 
139 void SchemeSmob::as_ref_count(SCM old_as, AtomSpace *nas)
140 {
141  AtomSpace* oas = ss_to_atomspace(old_as);
142  if (oas != nas)
143  {
144  std::lock_guard<std::mutex> lck(as_mtx);
145  if (deleteable_as.end() != deleteable_as.find(nas))
146  deleteable_as[nas]++;
147  if (oas and deleteable_as.end() != deleteable_as.find(oas))
148  deleteable_as[oas] --;
149  }
150 
151 }
152 
157 SCM SchemeSmob::ss_set_as (SCM new_as)
158 {
159  if (not SCM_SMOB_PREDICATE(SchemeSmob::cog_misc_tag, new_as))
160  return SCM_BOOL_F;
161 
162  if (COG_AS != SCM_SMOB_FLAGS(new_as))
163  return SCM_BOOL_F;
164 
165  SCM old_as = ss_get_as();
166  as_ref_count(old_as, ss_to_atomspace(new_as));
167 
168  scm_fluid_set_x(atomspace_fluid, new_as);
169 
170  return old_as;
171 }
172 
173 /* ============================================================== */
182 {
183  as_ref_count(ss_get_as(), nas);
184 
185  scm_fluid_set_x(atomspace_fluid, make_as(nas));
186 }
187 
189 {
190  SCM ref = scm_fluid_ref(atomspace_fluid);
191  AtomSpace* as = ss_to_atomspace(ref);
192  // if (NULL == as)
193  // scm_misc_error(subr, "No atomspace was specified!", SCM_BOOL_F);
194  return as;
195 }
196 
197 
198 /* ============================================================== */
199 
206 {
207  while (scm_is_pair(slist))
208  {
209  SCM sval = SCM_CAR(slist);
210  AtomSpace* as = ss_to_atomspace(sval);
211  if (as) return as;
212  slist = SCM_CDR(slist);
213  }
214 
215  return NULL;
216 }
217 
218 
219 #endif /* HAVE_GUILE */
220 /* ===================== END OF FILE ============================ */
static SCM ss_set_as(SCM)
static AtomSpace * get_as_from_list(SCM)
static scm_t_bits cog_misc_tag
Definition: SchemeSmob.h:59
#define BUFLEN
static SCM take_as(AtomSpace *)
Definition: SchemeSmobAS.cc:56
static void ss_set_env_as(AtomSpace *)
static SCM ss_as_p(SCM)
Definition: SchemeSmobAS.cc:84
static std::string as_to_string(const AtomSpace *)
Definition: SchemeSmobAS.cc:30
static SCM make_as(AtomSpace *)
Definition: SchemeSmobAS.cc:44
static std::map< AtomSpace *, int > deleteable_as
Definition: SchemeSmob.h:141
static void as_ref_count(SCM, AtomSpace *)
static AtomSpace * ss_to_atomspace(SCM)
static SCM atomspace_fluid
Definition: SchemeSmob.h:181
static SCM ss_new_as(SCM)
Definition: SchemeSmobAS.cc:69
static AtomSpace * ss_get_env_as(const char *)
static std::mutex as_mtx
Definition: SchemeSmob.h:140
static SCM ss_get_as(void)