1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "smx_private.h"
8 #include "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix,
14 "Logging specific to SIMIX (io)");
18 void SIMIX_pre_file_read(smx_simcall_t simcall)
20 smx_action_t action = SIMIX_file_read(simcall->issuer,
21 simcall->file_read.ptr,
22 simcall->file_read.size,
23 simcall->file_read.nmemb,
24 simcall->file_read.stream);
25 xbt_fifo_push(action->simcalls, simcall);
26 simcall->issuer->waiting_action = action;
29 smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size, size_t nmemb, smx_file_t* stream)
32 smx_host_t host = process->smx_host;
34 /* check if the host is active */
35 if (surf_workstation_model->extension.
36 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
37 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
41 action = xbt_mallocator_get(simix_global->action_mallocator);
42 action->type = SIMIX_ACTION_IO;
45 action->category = NULL;
48 action->io.host = host;
49 // TODO in surf model disk???
50 // action->io.surf_io = surf_workstation_model->extension.disk.read(host->host, name),
51 action->io.surf_io = surf_workstation_model->extension.workstation.sleep(host->host, 1.0);
53 surf_workstation_model->action_data_set(action->io.surf_io, action);
54 XBT_DEBUG("Create io action %p", action);
60 void SIMIX_pre_file_write(smx_simcall_t simcall)
62 smx_action_t action = SIMIX_file_write(simcall->issuer,
63 simcall->file_write.ptr,
64 simcall->file_write.size,
65 simcall->file_write.nmemb,
66 simcall->file_write.stream);
67 xbt_fifo_push(action->simcalls, simcall);
68 simcall->issuer->waiting_action = action;
71 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr, size_t size, size_t nmemb, smx_file_t* stream)
74 smx_host_t host = process->smx_host;
76 /* check if the host is active */
77 if (surf_workstation_model->extension.
78 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
79 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
83 action = xbt_mallocator_get(simix_global->action_mallocator);
84 action->type = SIMIX_ACTION_IO;
87 action->category = NULL;
90 action->io.host = host;
91 // TODO in surf model disk???
92 // action->io.surf_io = surf_workstation_model->extension.disk.write(host->host, name),
93 action->io.surf_io = surf_workstation_model->extension.workstation.sleep(host->host, 2.0);
95 surf_workstation_model->action_data_set(action->io.surf_io, action);
96 XBT_DEBUG("Create io action %p", action);
101 void SIMIX_post_io(smx_action_t action)
103 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
105 case SURF_ACTION_FAILED:
106 action->state = SIMIX_FAILED;
109 case SURF_ACTION_DONE:
110 action->state = SIMIX_DONE;
118 SIMIX_io_finish(action);
121 void SIMIX_io_destroy(smx_action_t action)
123 XBT_DEBUG("Destroy action %p", action);
124 if (action->io.surf_io)
125 action->io.surf_io->model_type->action_unref(action->io.surf_io);
126 xbt_mallocator_release(simix_global->action_mallocator, action);
129 void SIMIX_io_finish(smx_action_t action)
131 volatile xbt_fifo_item_t item;
132 smx_simcall_t simcall;
134 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
136 switch (action->state) {
139 /* do nothing, action done */
144 THROWF(io_error, 0, "IO failed");
146 CATCH(simcall->issuer->running_ctx->exception) {
147 simcall->issuer->doexception = 1;
153 THROWF(cancel_error, 0, "Canceled");
155 CATCH(simcall->issuer->running_ctx->exception) {
156 simcall->issuer->doexception = 1;
161 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
164 simcall->issuer->waiting_action = NULL;
165 SIMIX_simcall_answer(simcall);
168 /* We no longer need it */
169 SIMIX_io_destroy(action);