libdap Updated for version 3.21.0
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
42namespace libdap {
43
52class Locker {
53public:
54 Locker(pthread_mutex_t &lock, pthread_cond_t &cond, int &count);
55 virtual ~Locker();
56
57private:
58 pthread_mutex_t &m_mutex;
59
60 Locker();
61 Locker(const Locker &rhs);
62};
63
75public:
76 ChildLocker(pthread_mutex_t &lock, pthread_cond_t &cond, int &count);
77 virtual ~ChildLocker();
78
79private:
80 pthread_mutex_t &m_mutex;
81 pthread_cond_t &m_cond;
82 int &m_count;
83
85 ChildLocker(const Locker &rhs);
86};
87
96class MarshallerThread {
97private:
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,
127 int num)
128 : 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) {}
129
134 write_args(pthread_mutex_t &m, pthread_cond_t &c, int &count, std::string &e, int fd, char *vals, int num)
135 : d_mutex(m), d_cond(c), d_count(count), d_error(e), d_out(std::cerr), d_out_file(fd), d_buf(vals),
136 d_num(num) {}
137 };
138
139public:
140 MarshallerThread();
141 virtual ~MarshallerThread();
142
143 pthread_mutex_t &get_mutex() { return d_out_mutex; }
144 pthread_cond_t &get_cond() { return d_out_cond; }
145
146 int &get_child_thread_count() { return d_child_thread_count; }
147 void increment_child_thread_count() { ++d_child_thread_count; }
148
149 void start_thread(void *(*thread)(void *arg), std::ostream &out, char *byte_buf, unsigned int bytes_written);
150 void start_thread(void *(*thread)(void *arg), int fd, char *byte_buf, unsigned int bytes_written);
151
152 // These are static so they will have c-linkage - required because they
153 // are passed to pthread_create()
154 static void *write_thread(void *arg);
155 static void *write_thread_part(void *arg);
156};
157
158} // namespace libdap
159
160#endif /* MARSHALLERTHREAD_H_ */
ChildLocker(pthread_mutex_t &lock, pthread_cond_t &cond, int &count)
Locker(pthread_mutex_t &lock, pthread_cond_t &cond, int &count)
static void * write_thread(void *arg)
static void * write_thread_part(void *arg)
void start_thread(void *(*thread)(void *arg), std::ostream &out, char *byte_buf, unsigned int bytes_written)
top level DAP object to house generic methods
Definition AISConnect.cc:30