@@ -483,7 +483,15 @@ static ssize_t pipe_buffer_write(pipe_buffer_t *pipe_buffer, const char *src, si
483
483
/// @return 1 if the process is woken up, 0 if it remains in the wait queue.
484
484
int pipe_read_wake_function (wait_queue_entry_t * wait , unsigned mode , int sync )
485
485
{
486
+ // Retrieve private data associated with this wait queue entry.
486
487
pipe_inode_info_t * pipe_info = (pipe_inode_info_t * )wait -> private ;
488
+
489
+ // Check the private data.
490
+ if (pipe_info == NULL ) {
491
+ pr_err ("The private data is not a pipe_inode_info_t.\n" );
492
+ return -1 ;
493
+ }
494
+
487
495
// Validate that data is available in the pipe for reading.
488
496
if ((pipe_info_has_data (pipe_info ) > 0 ) || (pipe_info -> writers == 0 )) {
489
497
// Check if the task is in an appropriate sleep state to be woken up.
@@ -492,13 +500,13 @@ int pipe_read_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync)
492
500
wait -> task -> state = mode ;
493
501
494
502
// Signal that the task has been woken up.
495
- pr_debug ("pipe_read_wake_function: Data available or no more writers, waking up reader %d.\n" , wait -> task -> pid );
503
+ pr_debug ("Data available or no more writers, waking up reader %d.\n" , wait -> task -> pid );
496
504
return 1 ;
497
505
} else {
498
- pr_debug ("pipe_read_wake_function: Reader %d not in the correct state for wake-up.\n" , wait -> task -> pid );
506
+ pr_debug ("Reader %d not in the correct state for wake-up.\n" , wait -> task -> pid );
499
507
}
500
508
} else {
501
- pr_debug ("pipe_read_wake_function: No data available, reader %d should continue waiting.\n" , wait -> task -> pid );
509
+ pr_debug ("No data available, reader %d should continue waiting.\n" , wait -> task -> pid );
502
510
}
503
511
504
512
// No wake-up action taken, continue waiting.
@@ -512,21 +520,30 @@ int pipe_read_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync)
512
520
/// @return 1 if the process is woken up, 0 if it remains in the wait queue.
513
521
int pipe_write_wake_function (wait_queue_entry_t * wait , unsigned mode , int sync )
514
522
{
523
+ // Retrieve private data associated with this wait queue entry.
524
+ pipe_inode_info_t * pipe_info = (pipe_inode_info_t * )wait -> private ;
525
+
526
+ // Check the private data.
527
+ if (pipe_info == NULL ) {
528
+ pr_err ("The private data is not a pipe_inode_info_t.\n" );
529
+ return -1 ;
530
+ }
531
+
515
532
// Check if there is available space in the pipe for writing.
516
- if (pipe_info_has_space (( pipe_inode_info_t * ) wait -> private ) > 0 ) {
533
+ if (pipe_info_has_space (pipe_info ) > 0 ) {
517
534
// Only tasks in the state TASK_UNINTERRUPTIBLE or TASK_STOPPED can be woken up.
518
535
if ((wait -> task -> state == TASK_UNINTERRUPTIBLE ) || (wait -> task -> state == TASK_STOPPED )) {
519
536
// Set the wake-up mode for the task.
520
537
wait -> task -> state = mode ;
521
538
522
539
// Signal that the task has been woken up.
523
- pr_debug ("pipe_write_wake_function: Space available, waking up writer %d.\n" , wait -> task -> pid );
540
+ pr_debug ("Space available, waking up writer %d.\n" , wait -> task -> pid );
524
541
return 1 ;
525
542
} else {
526
- pr_debug ("pipe_write_wake_function: Writer %d not in the correct state for wake-up.\n" , wait -> task -> pid );
543
+ pr_debug ("Writer %d not in the correct state for wake-up.\n" , wait -> task -> pid );
527
544
}
528
545
} else {
529
- pr_debug ("pipe_write_wake_function: No space available, writer %d should continue waiting.\n" , wait -> task -> pid );
546
+ pr_debug ("No space available, writer %d should continue waiting.\n" , wait -> task -> pid );
530
547
}
531
548
532
549
// No wake-up action taken, continue waiting.
@@ -935,9 +952,6 @@ static ssize_t pipe_read(vfs_file_t *file, char *buffer, off_t offset, size_t nb
935
952
// Update the total bytes read and the read index.
936
953
bytes_read = bytes_read + bytes_to_read ;
937
954
pipe_info -> read_index = pipe_info -> read_index + bytes_to_read ;
938
-
939
- // Wake up tasks that might be waiting to write to the pipe.
940
- pipe_wake_up_tasks (& pipe_info -> write_wait , "pipe_read" );
941
955
}
942
956
} else {
943
957
// If in blocking mode, put the process to sleep until data is available.
@@ -952,6 +966,11 @@ static ssize_t pipe_read(vfs_file_t *file, char *buffer, off_t offset, size_t nb
952
966
// Release the mutex after reading.
953
967
mutex_unlock (& pipe_info -> mutex );
954
968
969
+ // Wake up tasks that might be waiting to write to the pipe.
970
+ if (bytes_read > 0 ) {
971
+ pipe_wake_up_tasks (& pipe_info -> write_wait , "pipe_read" );
972
+ }
973
+
955
974
return bytes_read ;
956
975
}
957
976
@@ -1019,9 +1038,6 @@ static ssize_t pipe_write(vfs_file_t *file, const void *buffer, off_t offset, si
1019
1038
// Update the total bytes written and the write index.
1020
1039
bytes_written = bytes_written + bytes_to_write ;
1021
1040
pipe_info -> write_index = pipe_info -> write_index + bytes_to_write ;
1022
-
1023
- // Wake up tasks waiting to read from the pipe.
1024
- pipe_wake_up_tasks (& pipe_info -> read_wait , "pipe_write" );
1025
1041
}
1026
1042
} else {
1027
1043
// Blocking behavior: Put the process to sleep until space is available.
@@ -1036,6 +1052,11 @@ static ssize_t pipe_write(vfs_file_t *file, const void *buffer, off_t offset, si
1036
1052
// Release the mutex after the write operation is complete.
1037
1053
mutex_unlock (& pipe_info -> mutex );
1038
1054
1055
+ // Wake up tasks waiting to read from the pipe.
1056
+ if (bytes_written > 0 ) {
1057
+ pipe_wake_up_tasks (& pipe_info -> read_wait , "pipe_write" );
1058
+ }
1059
+
1039
1060
return bytes_written ;
1040
1061
}
1041
1062
0 commit comments