attempting to support thread resource in http server

This commit is contained in:
hyung-hwan 2020-05-21 07:30:14 +00:00
parent aa5c70c3f5
commit 9ecec52f5f
4 changed files with 210 additions and 3 deletions

View File

@ -1508,13 +1508,54 @@ oops:
/* ----------------------------------------------------------------- */
#if 0
int mio_svc_htts_dothrfunc (mio_svc_htts_t* htts, mio_dev_sck_t* csck, mio_htre_t* req, mio_svc_htts_func_t func)
struct thr_state_t
{
MIO_SVC_HTTS_RSRC_HEADER;
pthread_t thr;
mio_svc_htts_thr_func_t func;
mio_dev_t* p1; /* change it to pipe */
};
typedef struct thr_state_t thr_state_t;
static void thr_state_on_kill (thr_state_t* thr_state)
{
}
static void* thr_state_run_func (void* arg)
{
thr_state_t* thr_state = (thr_state_t*)arg;
// thr_state->func (thr_state->XXX, thr_state->p1->fd); /* pass the pointer to data that can be accessed safely inside a thread. mostly these must be duplicates of core data */
return MIO_NULL;
}
int mio_svc_htts_dothr (mio_svc_htts_t* htts, mio_dev_sck_t* csck, mio_htre_t* req, mio_svc_htts_thr_func_t func)
{
mio_t* mio = htts->mio;
// mio_svc_htts_cli_t* cli = mio_dev_sck_getxtn(csck);
thr_state_t* thr_state = MIO_NULL;
thr_state = (thr_state_t*)mio_svc_htts_rsrc_make(htts, MIO_SIZEOF(*thr_state), thr_state_on_kill);
if (MIO_UNLIKELY(!thr_state)) goto oops;
thr_state->func = func;
#if 0
thr_state->p1 = mio_dev_pipe_make(mio);
if (MIO_UNLIKELY(!thr_state->p1)) goto oops;
#endif
if (pthread_create(&thr_state->thr, MIO_NULL, thr_state_run_func, thr_state) != 0)
{
}
return 0;
oops:
//if (thr_state) thr_state_halt_particiapting_devices (thr_state);
return -1;
}
/* ----------------------------------------------------------------- */

View File

@ -116,6 +116,8 @@ struct mio_svc_htts_rsrc_t
#define MIO_SVC_HTTS_RSRC_DETACH(rsrc_var) do { if (--(rsrc_var)->rsrc_refcnt == 0) { mio_svc_htts_rsrc_t* __rsrc_tmp = (rsrc_var); (rsrc_var) = MIO_NULL; mio_svc_htts_rsrc_kill(__rsrc_tmp); } else { (rsrc_var) = MIO_NULL; } } while(0)
/* -------------------------------------------------------------- */
typedef int (*mio_svc_htts_proc_req_t) (
mio_svc_htts_t* htts,
mio_dev_sck_t* sck,
@ -124,6 +126,14 @@ typedef int (*mio_svc_htts_proc_req_t) (
/* -------------------------------------------------------------- */
typedef int (*mio_svc_htts_thr_func_t) (
mio_svc_htts_t* htts,
mio_dev_sck_t* sck,
mio_htre_t* req
);
/* -------------------------------------------------------------- */
#if defined(__cplusplus)
extern "C" {
#endif
@ -249,6 +259,13 @@ MIO_EXPORT int mio_svc_htts_docgi (
const mio_bch_t* script
);
MIO_EXPORT int mio_svc_htts_dothr (
mio_svc_htts_t* htts,
mio_dev_sck_t* csck,
mio_htre_t* req,
mio_svc_htts_thr_func_t func
);
MIO_EXPORT mio_svc_htts_rsrc_t* mio_svc_htts_rsrc_make (
mio_svc_htts_t* htts,
mio_oow_t rsrc_size,

141
mio/lib/mio-pipe.h Normal file
View File

@ -0,0 +1,141 @@
/*
* $Id$
*
Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted pipevided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must repipeduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials pipevided with the distribution.
THIS SOFTWARE IS PIPEVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAfRRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PIPECUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PIPEFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _MIO_PIPE_H_
#define _MIO_PIPE_H_
#include <mio.h>
typedef struct mio_dev_pipe_t mio_dev_pipe_t;
typedef int (*mio_dev_pipe_on_read_t) (
mio_dev_pipe_t* dev,
const void* data,
mio_iolen_t len
);
typedef int (*mio_dev_pipe_on_write_t) (
mio_dev_pipe_t* dev,
mio_iolen_t wrlen,
void* wrctx
);
typedef void (*mio_dev_pipe_on_close_t) (
mio_dev_pipe_t* dev
);
struct mio_dev_pipe_t
{
MIO_DEV_HEADER;
int pfd[2];
mio_dev_pipe_on_read_t on_read;
mio_dev_pipe_on_write_t on_write;
mio_dev_pipe_on_close_t on_close;
};
typedef struct mio_dev_pipe_make_t mio_dev_pipe_make_t;
struct mio_dev_pipe_make_t
{
mio_dev_pipe_on_write_t on_write; /* mandatory */
mio_dev_pipe_on_read_t on_read; /* mandatory */
mio_dev_pipe_on_close_t on_close; /* optional */
};
enum mio_dev_pipe_ioctl_cmd_t
{
MIO_DEV_PIPE_CLOSE,
MIO_DEV_PIPE_KILL_CHILD
};
typedef enum mio_dev_pipe_ioctl_cmd_t mio_dev_pipe_ioctl_cmd_t;
#ifdef __cplusplus
extern "C" {
#endif
MIO_EXPORT mio_dev_pipe_t* mio_dev_pipe_make (
mio_t* mio,
mio_oow_t xtnsize,
const mio_dev_pipe_make_t* data
);
#if defined(MIO_HAVE_INLINE)
static MIO_INLINE mio_t* mio_dev_pipe_getmio (mio_dev_pipe_t* pipe) { return mio_dev_getmio((mio_dev_t*)pipe); }
#else
# define mio_dev_pipe_getmio(pipe) mio_dev_getmio(pipe)
#endif
#if defined(MIO_HAVE_INLINE)
static MIO_INLINE void* mio_dev_pipe_getxtn (mio_dev_pipe_t* pipe) { return (void*)(pipe + 1); }
#else
# define mio_dev_pipe_getxtn(pipe) ((void*)(((mio_dev_pipe_t*)pipe) + 1))
#endif
MIO_EXPORT void mio_dev_pipe_kill (
mio_dev_pipe_t* pipe
);
MIO_EXPORT void mio_dev_pipe_halt (
mio_dev_pipe_t* pipe
);
MIO_EXPORT int mio_dev_pipe_read (
mio_dev_pipe_t* pipe,
int enabled
);
MIO_EXPORT int mio_dev_pipe_timedread (
mio_dev_pipe_t* pipe,
int enabled,
const mio_ntime_t* tmout
);
MIO_EXPORT int mio_dev_pipe_write (
mio_dev_pipe_t* pipe,
const void* data,
mio_iolen_t len,
void* wrctx
);
MIO_EXPORT int mio_dev_pipe_timedwrite (
mio_dev_pipe_t* pipe,
const void* data,
mio_iolen_t len,
const mio_ntime_t* tmout,
void* wrctx
);
MIO_EXPORT int mio_dev_pipe_close (
mio_dev_pipe_t* pipe,
mio_dev_pipe_sid_t sid
);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -804,6 +804,14 @@ MIO_EXPORT int mio_dev_read (
int enabled
);
/*
* The mio_dev_timedread() function enables or disables the input watching.
* If tmout is not MIO_NULL, it schedules to fire the on_read() callback
* with the length of -1 and the mio error number set to MIO_ETMOUT.
* If there is input before the time elapses, the scheduled timer job
* is automaticaly cancelled. A call to mio_dev_read() or mio_dev_timedread()
* with no timeout also cancels the unfired scheduled job.
*/
MIO_EXPORT int mio_dev_timedread (
mio_dev_t* dev,
int enabled,