libdap  Updated for version 3.20.6
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 
113  struct write_args {
114  pthread_mutex_t &d_mutex;
115  pthread_cond_t &d_cond;
116  int &d_count;
117  std::string &d_error;
118  std::ostream &d_out; // The output stream protected by the mutex, ...
119  int d_out_file; // file descriptor; if not -1, use this.
120  char *d_buf; // The data to write to the stream
121  int d_num; // The size of d_buf
122 
126  write_args(pthread_mutex_t &m, pthread_cond_t &c, int &count, std::string &e, std::ostream &s, char *vals, int num) :
127  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)
128  {
129  }
130 
135  write_args(pthread_mutex_t &m, pthread_cond_t &c, int &count, std::string &e, int fd, char *vals, int num) :
136  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)
137  {
138  }
139  };
140 
141 public:
143  virtual ~MarshallerThread();
144 
145  pthread_mutex_t &get_mutex() { return d_out_mutex; }
146  pthread_cond_t &get_cond() { return d_out_cond; }
147 
148  int &get_child_thread_count() { return d_child_thread_count; }
149  void increment_child_thread_count() { ++d_child_thread_count; }
150 
151  void start_thread(void* (*thread)(void *arg), std::ostream &out, char *byte_buf, unsigned int bytes_written);
152  void start_thread(void* (*thread)(void *arg), int fd, char *byte_buf, unsigned int bytes_written);
153 
154  // These are static so they will have c-linkage - required because they
155  // are passed to pthread_create()
156  static void *write_thread(void *arg);
157  static void *write_thread_part(void *arg);
158 };
159 
160 }
161 
162 #endif /* MARSHALLERTHREAD_H_ */
top level DAP object to house generic methods
Definition: AISConnect.cc:30