1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
/*
* Dkses2.h
*
* $Id$
*
* Upper layer sessions
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2012 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "Dk.h"
static int
unfreeze_thread_read (dk_session_t * ses)
{
SESSION_SCH_DATA (ses)->sio_random_read_ready_action = NULL;
if (!SESSION_SCH_DATA (ses)->sio_default_read_ready_action)
remove_from_served_sessions (ses);
semaphore_leave (SESSION_SCH_DATA (ses)->sio_reading_thread->thr_sem);
return 0;
}
static int
unfreeze_thread_write (dk_session_t * ses)
{
SESSION_SCH_DATA (ses)->sio_random_write_ready_action = NULL;
/* in a direct io situation the session is not in the served set */
if (SESSION_SCH_DATA (ses)->sio_random_read_ready_action == NULL &&
SESSION_SCH_DATA (ses)->sio_default_read_ready_action == NULL)
{
/* if there is no other action on the session, remove it from served sessions. */
remove_from_served_sessions (ses);
}
ss_dprintf_4 (("Write in thread %p resumed.", (void *) (SESSION_SCH_DATA (ses)->sio_writing_thread)));
semaphore_leave (SESSION_SCH_DATA (ses)->sio_writing_thread->thr_sem);
return 0;
}
static void
freeze_thread_write (dk_session_t * ses)
{
USE_GLOBAL
SESSION_SCH_DATA (ses)->sio_random_write_ready_action = unfreeze_thread_write;
SESSION_SCH_DATA (ses)->sio_writing_thread = current_process;
add_to_served_sessions (ses);
ss_dprintf_4 (("Write on Thread %p blocked.", (void *) current_process));
semaphore_enter (current_process->thr_sem);
}
/*
* Used as random read ready action when a session is being
* directly read by a thread other than the server thread
*/
void
random_read_ready_while_direct_io (void)
{
PROCESS_ALLOW_SCHEDULE ();
}
int
service_write (dk_session_t * ses, char *buffer, int bytes)
{
USE_GLOBAL
int last_written = 0;
int rc;
DBG_CHECK_WRITE_FAIL (ses);
if (!ses->dks_session)
{
longjmp_splice (&SESSION_SCH_DATA (ses)->sio_write_broken_context, 1);
}
while (bytes > 0)
{
without_scheduling_tic ();
rc = session_write (ses->dks_session, &(buffer[last_written]), bytes);
restore_scheduling_tic ();
if (rc == 0)
PROCESS_ALLOW_SCHEDULE ();
if (rc > 0)
{
bytes = bytes - rc;
last_written = last_written + rc;
}
if (rc < 0)
{
if (SESSTAT_W_ISSET (ses->dks_session, SST_INTERRUPTED))
{
PROCESS_ALLOW_SCHEDULE ();
}
else if (SESSTAT_W_ISSET (ses->dks_session, SST_BLOCK_ON_WRITE))
{
if (!_thread_sched_preempt)
freeze_thread_write (ses);
else
{
timeout_t tv = { 100, 0 };
retry:
tcpses_is_write_ready (ses->dks_session, &tv);
if (SESSTAT_W_ISSET (ses->dks_session, SST_TIMED_OUT))
{
scheduler_io_data_t * sio = SESSION_SCH_DATA (ses);
if (sio->sio_w_timeout_hook && sio->sio_w_timeout_hook (ses))
{
SESSTAT_W_CLR (ses->dks_session, SST_TIMED_OUT);
goto retry;
}
SESSTAT_W_SET (ses->dks_session, SST_BROKEN_CONNECTION);
longjmp_splice (&SESSION_SCH_DATA (ses)->sio_write_broken_context, 1);
}
}
}
else
{
ses->dks_bytes_sent += last_written;
ss_dprintf_2 (("Unrecognized I/O error rc=%d errno=%d in service_write", rc, errno));
SESSTAT_W_CLR (ses->dks_session, SST_OK);
SESSTAT_W_SET (ses->dks_session, SST_BROKEN_CONNECTION);
longjmp_splice (&SESSION_SCH_DATA (ses)->sio_write_broken_context, 1);
}
}
}
ses->dks_bytes_sent += last_written;
return 0;
}
/*##**********************************************************************
* session_flush
*
* This writes out the data in the session's output buffer. This operation
* does not make sense for a string output session. Therefore check.
*
* Assumes that caller holds dks_mtx.
*
* Input params : - dk_session
*
* Output params: -
*
* Return value : SER_SUCC if write was successful.
* SER_FAIL if not. If failed the status of the
* session is set accordingly.
*
* Limitations :
*
* Globals used : default controls
*/
int
session_flush_1 (dk_session_t * ses)
{
if (!ses->dks_session || (ses->dks_session->ses_class == SESCLASS_STRING && !ses->dks_session->ses_file->ses_file_descriptor))
return (SER_SUCC);
if (ses->dks_out_fill)
{
int rc = service_write (ses, ses->dks_out_buffer, ses->dks_out_fill);
ses->dks_out_fill = 0;
return (rc);
}
return (SER_SUCC);
}
int
session_flush (dk_session_t * session)
{
int ret = SER_SUCC;
if (NULL != session->dks_mtx)
mutex_enter (session->dks_mtx);
CATCH_WRITE_FAIL (session)
{
session_flush_1 (session);
ret = SER_SUCC;
}
FAILED
{
ret = SER_FAIL;
}
END_WRITE_FAIL (session);
if (NULL != session->dks_mtx)
mutex_leave (session->dks_mtx);
return (ret);
}
/*##**********************************************************************
* session_buffered_write
*
* This adds the string to the output buffer. The output buffer
* if flushed if needed.
*
* Input params : - session
* pointer to string to write
* bytes to write
*
* Output params: -
*
* Return value : SER_SUCC if write was successful.
* SER_FAIL if not. If failed the status of the
* session is set accordingly.
*
* Limitations :
*
* Globals used : default controls
*/
int
session_buffered_write (dk_session_t * ses, const char *buffer, size_t _length)
{
int length = (int) _length;
DBG_CHECK_WRITE_FAIL (ses);
/* will the string fit ? */
if (length <= ses->dks_out_length - ses->dks_out_fill)
{
memcpy (&ses->dks_out_buffer[ses->dks_out_fill], buffer, length);
ses->dks_out_fill = ses->dks_out_fill + length;
}
else
{
/* fill the buffer, write out and write the rest in one call */
int written;
if (!ses->dks_session)
{
ses->dks_out_fill = ses->dks_out_length;
return 0;
}
if (strses_is_utf8 (ses))
{
written = utf8_align_memcpy (&ses->dks_out_buffer[ses->dks_out_fill], buffer,
ses->dks_out_length - ses->dks_out_fill, NULL, NULL);
if (written == -1)
{
SESSTAT_CLR (ses->dks_session, SST_OK);
SESSTAT_SET (ses->dks_session, SST_BROKEN_CONNECTION);
longjmp_splice (&SESSION_SCH_DATA (ses)->sio_write_broken_context, 1);
}
service_write (ses, ses->dks_out_buffer, ses->dks_out_fill + written);
}
else
{
memcpy (&ses->dks_out_buffer[ses->dks_out_fill], buffer,
written = ses->dks_out_length - ses->dks_out_fill);
service_write (ses, ses->dks_out_buffer, ses->dks_out_length);
}
if (length - written > ses->dks_out_length)
{
service_write (ses, (char *) &buffer[written], length - written);
ses->dks_out_fill = 0;
}
else
{
memcpy (ses->dks_out_buffer, &buffer[written], length - written);
ses->dks_out_fill = length - written;
}
}
if (ses->dks_session && ses->dks_session->ses_file && ses->dks_session->ses_file->ses_file_descriptor)
session_flush_1 (ses);
return 0;
}
#if 0
void
session_buffered_write_char (unsigned char ch, dk_session_t * ses)
{
if (ses->dks_out_fill >= ses->dks_out_length)
{
if (!ses->dks_session)
return;
service_write (ses, ses->dks_out_buffer, ses->dks_out_fill);
ses->dks_out_buffer[0] = ch;
ses->dks_out_fill = 1;
}
else
{
ses->dks_out_buffer[ses->dks_out_fill] = ch;
ses->dks_out_fill++;
}
}
#else
void
session_buffered_write_char (int c, dk_session_t * ses)
{
dtp_t ch = (dtp_t) c;
if (ses->dks_out_fill >= ses->dks_out_length)
{
if (!ses->dks_session)
return;
service_write (ses, ses->dks_out_buffer, ses->dks_out_fill);
ses->dks_out_buffer[0] = ch;
ses->dks_out_fill = 1;
}
else
ses->dks_out_buffer[ses->dks_out_fill++] = ch;
}
#endif
/*
service_read ()
Used to read from a session. Handles scheduling
if the session would block.
Used for reading from a service thread. If the read would block,
put the thread to wait. When the scheduling cycle sees input on this
session the random_input_ready_action is called.
This wakes up this thread and schedules it for execution on the next
round
The need_all argument controls whether this function may return after reading
fewer than the requested number of bytes. This function always reads at
least 1 byte.
If the calling thread is the scheduling thread and io would block, this
allows schedule and recursively blocks on all pending i/o.
If the calling thread is some other thread, this disables
the thread and tells the scheduler to resume this when the input is ready.
Returns the number of bytes read.
*/
int
service_read (dk_session_t * ses, char *buffer, int req_bytes, int need_all)
{
USE_GLOBAL
int last_read = 0;
int bytes = req_bytes;
du_thread_t *cur_proc; /* mty NEW */
int rc;
DBG_CHECK_READ_FAIL (ses);
while (bytes > 0)
{
without_scheduling_tic ();
if (!ses->dks_is_read_select_ready && ses->dks_session && ses->dks_session->ses_class != SESCLASS_STRING)
{
tcpses_is_read_ready (ses->dks_session, &ses->dks_read_block_timeout);
if (DKSESSTAT_ISSET (ses, SST_TIMED_OUT))
rc = -1;
else
rc = session_read (ses->dks_session, &(buffer[last_read]), bytes);
}
else
{
if (!ses->dks_session)
longjmp_splice (&(SESSION_SCH_DATA (ses)->sio_read_broken_context), 1);
rc = session_read (ses->dks_session, &(buffer[last_read]), bytes);
}
ses->dks_is_read_select_ready = 0;
restore_scheduling_tic ();
if (rc == 0)
PROCESS_ALLOW_SCHEDULE ();
else if (rc > 0)
{
bytes = bytes - rc;
last_read = last_read + rc;
if (!need_all)
{
ses->dks_bytes_received += last_read;
return (last_read);
}
}
if (rc <= 0)
{
if (SESSTAT_ISSET (ses->dks_session, SST_INTERRUPTED))
{
PROCESS_ALLOW_SCHEDULE ();
}
else if (SESSTAT_ISSET (ses->dks_session, SST_BLOCK_ON_READ))
{
/* would block. suspend thread */
cur_proc = current_process; /* mty NEW */
if (!PROCESS_TO_DK_THREAD (cur_proc))
{
/* We have a block on a server thread. We recognize it
* because a server thread is not associated to a request.
* The read would block the server thread. Run others and
* do a recursive check_inputs to resume other threads that
* may now be ready for i/o. Do a timeout round to unblock
* threads waiting on timed-out futures if the select times
* out. Finally retry read.
*/
int rc2;
PROCESS_ALLOW_SCHEDULE ();
rc2 = check_inputs (PASS_G & atomic_timeout, 1);
if (rc2 == 0)
timeout_round (PASS_G ses);
}
else
{
SESSION_SCH_DATA (ses)->sio_random_read_ready_action = unfreeze_thread_read;
SESSION_SCH_DATA (ses)->sio_reading_thread = cur_proc;
add_to_served_sessions (ses);
semaphore_enter (cur_proc->thr_sem);
}
}
else if (1 || /* ?? */
SESSTAT_ISSET (ses->dks_session, SST_TIMED_OUT) || SESSTAT_ISSET (ses->dks_session, SST_BROKEN_CONNECTION))
{
SESSTAT_CLR (ses->dks_session, SST_OK);
SESSTAT_SET (ses->dks_session, SST_BROKEN_CONNECTION);
longjmp_splice (&(SESSION_SCH_DATA (ses)->sio_read_broken_context), 1);
}
else
{
ses->dks_bytes_received += last_read;
ss_dprintf_2 (("Unrecognized I/O error rc=%d errno=%d in service_read.", rc, errno));
longjmp_splice (&(SESSION_SCH_DATA (ses)->sio_read_broken_context), 1);
}
}
}
ses->dks_bytes_received += last_read;
return (last_read);
}
/*##**********************************************************************
* session_buffered_read
*
* If input buffer contains enough data, copies the data into the buffer
* Else :
* 1) if we want more than a buffer full, we read straight into the target.
* 2) if we want less, we fill the buffer until there is enough and then copy
* the data into the target and leave whatever else was read in the buffer.
* I/O errors are handled in service_read. This always returns successfully.
*
* Input params : - session, buffer, byte count.
*
* Output params: - buffer, session updated
*
* Return value : bytes read.
*
* Limitations :
*
* Globals used : default controls
*/
int
session_buffered_read (dk_session_t * ses, char *buffer, int req_bytes)
{
if (ses->dks_in_fill - ses->dks_in_read >= req_bytes)
{
memcpy (buffer, &ses->dks_in_buffer[ses->dks_in_read], req_bytes);
ses->dks_in_read = ses->dks_in_read + req_bytes;
return (req_bytes);
}
else
{
int bytes_read = 0;
int bytes_from_previous;
/* Move the stuff in the buffer to target.
If there's more than a buffer full needed, read into the target
if less, fill the buffer. */
memcpy (buffer, &ses->dks_in_buffer[ses->dks_in_read],
bytes_from_previous = bytes_read = ses->dks_in_fill - ses->dks_in_read);
ses->dks_in_read = ses->dks_in_fill;
if (req_bytes > ses->dks_in_length)
{
int rc = service_read (ses, &buffer[bytes_read],
req_bytes - bytes_read, 1);
if (rc < 0)
return (rc);
else
return (req_bytes);
}
else
{
int rc;
int bytes_to_fill = ses->dks_in_length;
int bytes_filled = 0;
while (1)
{
rc = service_read (ses, &ses->dks_in_buffer[bytes_filled], bytes_to_fill, 0);
if (rc > 0)
{
bytes_read = bytes_read + rc;
bytes_filled = bytes_filled + rc;
bytes_to_fill = bytes_to_fill - rc;
if (bytes_read >= req_bytes)
{
ses->dks_in_fill = bytes_filled;
ses->dks_in_read = req_bytes - bytes_from_previous;
memcpy (&buffer[bytes_from_previous], ses->dks_in_buffer, ses->dks_in_read);
return (req_bytes);
}
}
else
{
return (rc);
}
}
}
}
}
/*##**********************************************************************
* session_buffered_read_char
*
* Reads one character fro the session's input buffer. If buffer empty
* fills buffer.
*
* Input params :
*
* ses session to read.
*
* Output params: - none
*
* Return value : character read.
*
*
* Limitations :
*
*
* Globals used : - none
*/
dtp_t
session_buffered_read_char (dk_session_t * ses)
{
#if 0
dtp_t c;
if (ses->dks_in_read < ses->dks_in_fill)
return (dtp_t) ses->dks_in_buffer[ses->dks_in_read++];
session_buffered_read (ses, &c, (size_t) 1);
return c;
#else
char c;
int point = ses->dks_in_read;
DBG_CHECK_READ_FAIL (ses);
if (point < ses->dks_in_fill)
{
ses->dks_in_read = point + 1;
return (ses->dks_in_buffer[point]);
}
else
{
session_buffered_read (ses, &c, 1);
return (c);
}
#endif
}
|