libdap  Updated for version 3.19.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
MarshallerThread.h
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2015 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 /*
27  * MarshallerThread.h
28  *
29  * Created on: Aug 27, 2015
30  * Author: jimg
31  */
32 
33 #ifndef MARSHALLERTHREAD_H_
34 #define MARSHALLERTHREAD_H_
35 
36 #include <pthread.h>
37 
38 #include <iostream>
39 #include <ostream>
40 #include <string>
41 
42 namespace libdap {
43 
52 class Locker {
53 public:
54  Locker(pthread_mutex_t &lock, pthread_cond_t &cond, int &count);
55  virtual ~Locker();
56 
57 private:
58  pthread_mutex_t& m_mutex;
59 
60  Locker();
61  Locker(const Locker &rhs);
62 };
63 
74 class ChildLocker {
75 public:
76  ChildLocker(pthread_mutex_t &lock, pthread_cond_t &cond, int &count);
77  virtual ~ChildLocker();
78 
79 private:
80  pthread_mutex_t& m_mutex;
81  pthread_cond_t& m_cond;
82  int& m_count;
83 
84  ChildLocker();
85  ChildLocker(const Locker &rhs);
86 };
87 
97 private:
98  pthread_t d_thread;
99  pthread_attr_t d_thread_attr;
100 
101  pthread_mutex_t d_out_mutex;
102  pthread_cond_t d_out_cond;
103 
104  int d_child_thread_count; // 0 or 1
105  std::string d_thread_error; // non-null indicates an error
106 
107 #if 0
108  static bool print_time; // false by default
109 #endif
110 
117  struct write_args {
118  pthread_mutex_t &d_mutex;
119  pthread_cond_t &d_cond;
120  int &d_count;
121  std::string &d_error;
122  std::ostream &d_out; // The output stream protected by the mutex, ...
123  int d_out_file; // file descriptor; if not -1, use this.
124  char *d_buf; // The data to write to the stream
125  int d_num; // The size of d_buf
126 
130  write_args(pthread_mutex_t &m, pthread_cond_t &c, int &count, std::string &e, std::ostream &s, char *vals, int num) :
131  d_mutex(m), d_cond(c), d_count(count), d_error(e), d_out(s), d_out_file(-1), d_buf(vals), d_num(num)
132  {
133  }
134 
139  write_args(pthread_mutex_t &m, pthread_cond_t &c, int &count, std::string &e, int fd, char *vals, int num) :
140  d_mutex(m), d_cond(c), d_count(count), d_error(e), d_out(std::cerr), d_out_file(fd), d_buf(vals), d_num(num)
141  {
142  }
143  };
144 
145 public:
147  virtual ~MarshallerThread();
148 
149  pthread_mutex_t &get_mutex() { return d_out_mutex; }
150  pthread_cond_t &get_cond() { return d_out_cond; }
151 
152  int &get_child_thread_count() { return d_child_thread_count; }
153  void increment_child_thread_count() { ++d_child_thread_count; }
154 
155  void start_thread(void* (*thread)(void *arg), std::ostream &out, char *byte_buf, unsigned int bytes_written);
156  void start_thread(void* (*thread)(void *arg), int fd, char *byte_buf, unsigned int bytes_written);
157 
158  // These are static so they will have c-linkage - required because they
159  // are passed to pthread_create()
160  static void *write_thread(void *arg);
161  static void *write_thread_part(void *arg);
162 
163 #if 0
164  static void set_print_time(bool state) { print_time = state; }
165  static bool get_print_time() { return print_time; }
166 #endif
167 
168 };
169 
170 }
171 
172 #endif /* MARSHALLERTHREAD_H_ */