Merged gcell-wip -r8159:8202 into trunk. This includes the following
[debian/gnuradio] / gcell / src / lib / runtime / gc_job_manager.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include "gc_job_manager.h"
26 #include "gc_job_manager_impl.h"
27 #include <boost/weak_ptr.hpp>
28 #include <stdio.h>
29
30
31 static boost::weak_ptr<gc_job_manager> s_singleton;
32
33
34 gc_job_manager_sptr
35 gc_make_job_manager(const gc_jm_options *options)
36 {
37   return gc_job_manager_sptr(new gc_job_manager_impl(options));
38 }
39
40 gc_job_manager::gc_job_manager(const gc_jm_options *options)
41 {
42   // nop
43 }
44
45 gc_job_manager::~gc_job_manager()
46 {
47   // nop
48 }
49
50 void
51 gc_job_manager::set_debug(int debug)
52 {
53   // nop
54 }
55
56 int
57 gc_job_manager::debug()
58 {
59   return 0;
60 }
61
62 void 
63 gc_job_manager::set_singleton(gc_job_manager_sptr mgr)
64 {
65   s_singleton = mgr;
66 }
67
68 gc_job_manager_sptr 
69 gc_job_manager::singleton()
70 {
71   return gc_job_manager_sptr(s_singleton);
72 }
73
74 // ------------------------------------------------------------------------
75
76
77 // custom deleter
78 class spe_program_handle_deleter {
79 public:
80   void operator()(spe_program_handle_t *program) {
81     if (program){
82       int r = spe_image_close(program);
83       if (r != 0){
84         perror("spe_image_close");
85       }
86     }
87   }
88 };
89
90 // nop custom deleter
91 class nop_spe_program_handle_deleter {
92 public:
93   void operator()(spe_program_handle_t *program) {
94   }
95 };
96
97 spe_program_handle_sptr 
98 gc_program_handle_from_filename(const std::string &filename)
99 {
100   return spe_program_handle_sptr(spe_image_open(filename.c_str()),
101                                  spe_program_handle_deleter());
102 }
103
104
105 spe_program_handle_sptr 
106 gc_program_handle_from_address(spe_program_handle_t *handle)
107 {
108   return spe_program_handle_sptr(handle, nop_spe_program_handle_deleter());
109 }
110
111 const std::string
112 gc_job_status_string(gc_job_status_t status)
113 {
114   switch(status){
115   case JS_OK:                   return "JS_OK";
116   case JS_SHUTTING_DOWN:        return "JS_SHUTTING_DOWN";
117   case JS_TOO_MANY_CLIENTS:     return "JS_TOO_MANY_CLIENTS";
118   case JS_UNKNOWN_PROC:         return "JS_UNKNOWN_PROC";
119   case JS_BAD_DIRECTION:        return "JS_BAD_DIRECTION";
120   case JS_BAD_EAH:              return "JS_BAD_EAH";
121   case JS_BAD_N_DIRECT:         return "JS_BAD_N_DIRECT";
122   case JS_BAD_N_EA:             return "JS_BAD_N_EA";
123   case JS_ARGS_TOO_LONG:        return "JS_ARGS_TOO_LONG";
124   case JS_BAD_JUJU:             return "JS_BAD_JUJU";
125   case JS_BAD_JOB_DESC:         return "JS_BAD_JOB_DESC";
126   default:
127     char buf[100];
128     snprintf(buf, sizeof(buf), "unknown gc_job_status_t (%d)\n", status);
129     return buf;
130   }
131 }
132
133 /*
134  * exception classes
135  */
136
137 gc_exception::gc_exception(const std::string &msg)
138   : runtime_error(msg)
139 {
140 }
141
142 gc_unknown_proc::gc_unknown_proc(const std::string &msg)
143   : gc_exception("gc_unknown_proc: " + msg)
144 {
145 }
146
147 gc_bad_alloc::gc_bad_alloc(const std::string &msg)
148   : gc_exception("gc_bad_alloc: " + msg)
149 {
150 }
151
152 gc_bad_align::gc_bad_align(const std::string &msg)
153   : gc_exception("gc_bad_align: " + msg)
154 {
155 }
156
157 gc_bad_submit::gc_bad_submit(const std::string &name, gc_job_status_t status)
158   : gc_exception("gc_bad_submit(" + name + "): " + gc_job_status_string(status))
159 {
160 }